用pillow把多张image合成为pdf

0 Comments

from PIL import Image
import os

def picture2pdf( source_images, pdf_file_name ):
    """把source_images下面的图片合成pdf文件,请根据具体情况修改代码"""
    
    files = os.listdir( source_images )
    amounts = len(files)                        # 统计数量
    image_files = []

    for i in range(1,amounts+1):
        file = '幻灯片' + str(i) + ".JPG"
        image_files.append( source_images + file )
        
    output = Image.open(image_files[0])
    剩下的 = [Image.open(file) for file in image_files[1:] ]     
    
    output.save( pdf_file_name, "pdf", save_all=True, append_images=剩下的 )

if __name__ == "__main__":
    folder = os.getcwd() + os.sep + "pic" + os.sep
    pdfFile = "Python海龟手册_print.pdf"
    picture2pdf( folder, pdfFile )

发表评论