存在问题
当用Adobe Acrobat DC想将图片转换成PDF的时候,有时候会报错,如下:
多次尝试还是出现这个问题。
解决方案
- 基于Python代码实现
from PIL import Image
import os
def images_to_pdf(input_folder, output_pdf):
"""
将指定文件夹中的所有图像文件批量转换为单个PDF文件,并确保页面大小一致且图像质量保持不变。
参数:
input_folder (str): 图像文件所在的文件夹路径。
output_pdf (str): 输出的 PDF 文件路径。
"""
# 获取输入文件夹中所有的图像文件
image_files = [f for f in os.listdir(input_folder) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))]
image_files.sort() # 排序以按文件名顺序添加到PDF中
# 确保至少有一个图像文件
if not image_files:
print("No image files found in the specified folder.")
return
# 打开所有图像并转换为相同大小的RGB格式图像
images = []
for img_file in image_files:
img_path = os.path.join(input_folder, img_file)
image = Image.open(img_path).convert('RGB')
# 记录所有图像的最大宽度和高度
images.append(image)
# 确定所有图像的目标大小(使用第一个图像的大小作为标准)
width, height = images[0].size
# 将所有图像调整为目标大小
resized_images = [img.resize((width, height), Image.LANCZOS) for img in images]
# 将所有图像保存到单个PDF文件中
resized_images[0].save(output_pdf, save_all=True, append_images=resized_images[1:])
print(f"Successfully converted images to {output_pdf}.")
# 示例用法
input_folder = './imgs' # 替换为你的图像文件夹路径
output_pdf = 'output.pdf' # 替换为你想要保存的PDF文件名
images_to_pdf(input_folder, output_pdf)
2. 附上小程序
嫌麻烦配置Python的,可以直接用小程序,解压文件夹,点击exe进行操作