import os
from PIL import Image
import io
def convert_image_to_pdf(image_path, output_path):
try:
if not os.path.exists(image_path):
raise FileNotFoundError(f"Input file {image_path} does not exist")
if not image_path.endswith(('.png', '.jpg', '.jpeg')):
raise TypeError(f"Input file {image_path} is not a valid image file")
image = Image.open(image_path)
image = image.convert('RGB')
pdf = io.BytesIO()
image.save(pdf, format='PDF')
with open(output_path, 'wb') as file:
file.write(pdf.getvalue())
except Exception as e:
print(f"Error occurred: {e}")
convert_image_to_pdf("signature_5.png", "result_202310221115.pdf")
Python代码:将图像转换为PDF
最新推荐文章于 2024-09-14 08:25:16 发布