Python实现批量将图片转为PDF文件的示例代码

近期有些网友想要了解Python实现批量将图片转为PDF文件的示例代码的相关情况,小编通过整理给您分析,同时介绍一下有关信息。

在日常工作中,我们经常需要将多张图片转换为PDF文件,以便于分享和存档。手动操作不仅耗时,而且容易出错。幸运的是,Python 提供了强大的库,如Pillow和PyPDF2,可以帮助我们轻松实现这一任务。本文将详细介绍如何使用Python批量将图片转换为PDF文件,并提供完整的示例代码,帮助读者快速上手。

准备工作

在开始之前,需要确保已经安装了所需的Python库。将使用Pillow库来处理图像,并使用PyPDF2库来生成PDF文件。

安装Pillow和PyPDF2

可以使用pip命令来安装这些库:

pipinstallPillowPyPDF2

使用Pillow将图片转换为PDF

Pillow是Python Imaging Library(PIL)的一个分支,是一个非常强大的图像处理库。它支持打开、操作和保存多种格式的图像。

单张图片转换为PDF

首先,来看如何将单张图片转换为PDF。

fromPILimportImage

defimage_to_pdf(image_path,output_path):
image=Image.open(image_path)
pdf_path=output_path
image.convert('RGB').save(pdf_path)
print(f"已将图片{image_path}转换为PDF文件{pdf_path}")

#示例
image_to_pdf('example.jpg','output.pdf')

在这个示例中,打开了一张图片并将其转换为RGB模式,然后保存为PDF文件。

多张图片转换为PDF

接下来,将多张图片合并到一个PDF文件中。

fromPILimportImage

defimages_to_pdf(image_paths,output_path):
images=[Image.open(image).convert('RGB')forimageinimage_paths]
images[0].save(output_path,save_all=True,append_images=images[1:])
print(f"已将多张图片合并为PDF文件{output_path}")

#示例
image_paths=['image1.jpg','image2.jpg','image3.jpg']
images_to_pdf(image_paths,'merged_output.pdf')

在这个示例中,首先将所有图片转换为RGB模式,然后使用save_all=True选项将它们保存为一个PDF文件。

批量处理文件夹中的图片

在实际应用中,可能需要将一个文件夹中的所有图片批量转换为PDF。可以使用os库来遍历文件夹中的所有图片。

importos
fromPILimportImage

deffolder_to_pdf(folder_path,output_path):
image_paths=[]
forfile_nameinos.listdir(folder_path):
iffile_name.endswith(('jpg','jpeg','png')):
image_paths.append(os.path.join(folder_path,file_name))

ifimage_paths:
images=[Image.open(image).convert('RGB')forimageinsorted(image_paths)]
images[0].save(output_path,save_all=True,append_images=images[1:])
print(f"已将文件夹{folder_path}中的图片合并为PDF文件{output_path}")
else:
print("文件夹中没有找到图片文件")

#示例
folder_to_pdf('images_folder','output_folder.pdf')

在这个示例中,遍历指定文件夹中的所有图片,并将它们合并为一个PDF文件。

处理不同格式的图片

有时,需要处理不同格式的图片,如PNG、BMP等。Pillow支持多种图像格式,可以轻松处理这些图片。

importos
fromPILimportImage

deffolder_to_pdf(folder_path,output_path):
image_paths=[]
forfile_nameinos.listdir(folder_path):
iffile_name.endswith(('jpg','jpeg','png','bmp','tiff')):
image_paths.append(os.path.join(folder_path,file_name))

ifimage_paths:
images=[Image.open(image).convert('RGB')forimageinsorted(image_paths)]
images[0].save(output_path,save_all=True,append_images=images[1:])
print(f"已将文件夹{folder_path}中的图片合并为PDF文件{output_path}")
else:
print("文件夹中没有找到图片文件")

#示例
folder_to_pdf('images_folder','output_folder.pdf')

添加图像压缩和调整

在处理大量图片时,可能需要对图片进行压缩或调整大小,以减少PDF文件的大小。

importos
fromPILimportImage

defresize_image(image,max_size):
ratio=min(max_size/image.width,max_size/image.height)
new_width=int(image.width*ratio)
new_height=int(image.height*ratio)
returnimage.resize((new_width,new_height),Image.ANTIALIAS)

deffolder_to_pdf(folder_path,output_path,max_size=1000):
image_paths=[]
forfile_nameinos.listdir(folder_path):
iffile_name.endswith(('jpg','jpeg','png','bmp','tiff')):
image_paths.append(os.path.join(folder_path,file_name))

ifimage_paths:
images=[resize_image(Image.open(image).convert('RGB'),max_size)forimageinsorted(image_paths)]
images[0].save(output_path,save_all=True,append_images=images[1:])
print(f"已将文件夹{folder_path}中的图片合并为PDF文件{output_path}")
else:
print("文件夹中没有找到图片文件")

#示例
folder_to_pdf('images_folder','output_folder.pdf')

在这个示例中,添加了一个resize_image函数,用于将图片调整到指定大小以内。

完整示例

下面是一个完整的示例代码,将所有步骤综合在一起,实现图片批量转换为PDF的功能。

importos
fromPILimportImage

defresize_image(image,max_size):
ratio=min(max_size/image.width,max_size/image.height)
new_width=int(image.width*ratio)
new_height=int(image.height*ratio)
returnimage.resize((new_width,new_height),Image.ANTIALIAS)

deffolder_to_pdf(folder_path,output_path,max_size=1000):
image_paths=[]
forfile_nameinos.listdir(folder_path):
iffile_name.endswith(('jpg','jpeg','png','bmp','tiff')):
image_paths.append(os.path.join(folder_path,file_name))

ifimage_paths:
images=[resize_image(Image.open(image).convert('RGB'),max_size)forimageinsorted(image_paths)]
images[0].save(output_path,save_all=True,append_images=images[1:])
print(f"已将文件夹{folder_path}中的图片合并为PDF文件{output_path}")
else:
print("文件夹中没有找到图片文件")

#示例
folder_to_pdf('images_folder','output_folder.pdf')

总结

通过本文的介绍,我们了解了如何使用Python中的Pillow和PyPDF2库将多张图片批量转换为PDF文件。从单张图片的转换到多张图片的合并,再到批量处理文件夹中的所有图片,每一步都详细讲解并提供了相应的代码示例。此外,我们还讨论了如何处理不同格式的图片以及如何对图片进行压缩和调整大小,以减少生成的PDF文件的大小。希望本文的内容能够帮助读者高效地完成图片到PDF的转换任务,提高工作效率。

发布于 2025-01-14 03:28:17
分享
海报
132
上一篇:使用Python和Tkinter的开发股票交易计算器 下一篇:C++函数模板(Template)基础知识讲解
目录

    忘记密码?

    图形验证码