使用 FPDF 将图像转换为 pdf 时的页面方向 (python)

Page Orientation when converting images to pdf using FPDF (python)

我正在尝试使用 FPDF 将图像文件夹 (jpg) 转换为 PDF。此代码非常适合纵向格式的图像。对于风景中的图像,尽管它将它们置于肖像格式中。有没有办法检测方向并将其分配给 pdf?

已更新修复代码

from fpdf import FPDF
from PIL import Image
import glob
import os

image_directory = r'C:\coolbro\test\yay\test'

extensions = ('*.jpg','*.png','*.gif')

imagelist=[]
for ext in extensions:
    imagelist.extend(glob.glob(os.path.join(image_directory,ext)))

for imagePath in imagelist:


    cover = Image.open(imagePath)
    width, height = cover.size

    if height > width:

        pdf = FPDF(unit = "pt", format = "legal")

        pdf.add_page()

        pdf.image(imagePath, 0, 0, 600)

        destination = os.path.splitext(imagePath)[0]
        pdf.output(destination + ".pdf", "F")

    if width > height:

        pdf = FPDF("L", unit = "pt", format = "legal")

        pdf.add_page()

        pdf.image(imagePath, 0, 0, 0, 600)

        destination = os.path.splitext(imagePath)[0]
        pdf.output(destination + ".pdf", "F")

Comment: I was trying to find a way to read the orientation

if Y > X:
    # portrait
else:
    # landscape

添加orientation = 'L',例如:

fpdf = FPDF(orientation = 'L', unit = 'mm', format='A4')