[Python] python FPDF not sizing correctly

Discussão em 'Python' iniciado por Stack, Abril 21, 2025.

  1. Stack

    Stack Membro Participativo

    I am getting a list of images from a directory and I am trying to convert a list of images to PDF. I am getting their width and height and using Image module. When the program runs and I open the PDF file, the picture look very big and only a corner of the pic.

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

    image_directory = '/Users/myuser/pics/'
    extensions = ('*.jpg','*.png','*.gif')
    pdf = FPDF()
    imagelist=[]
    for ext in extensions:
    imagelist.extend(glob.glob(os.path.join(image_directory,ext)))

    for imageFile in imagelist:
    cover = Image.open(imageFile)
    width, height = cover.size
    pdf.add_page()
    # 1 px = 0.264583 mm (FPDF default is mm)
    pdf.image(imageFile, 0, 0, float(width * 0.264583), float(height * 0.264583))
    pdf.output(image_directory + "file.pdf", "F")


    The image is the left one and the right is the PDF [​IMG]

    Continue reading...

Compartilhe esta Página