AlexGilvarry AlexGilvarry - 1 year ago 374
Python Question

Python Image Library convert from Jpeg to PDF

I'm attempting to convert a Jpeg file with, 200 dpi, to a PDF file, however, when I save the file as a PDF I think it's changing the dpi to 72, and thus making the image larger. I had a similar problem when initially trying to scale my jpeg image to a smaller size, and was able to solve that by specifying the dpi when I save the image.

im ="Image.jpg")['dpi']

if im.size == (2592, 1728):
out = im.resize((1188,792), Image.ANTIALIAS)
elif im.size == (1728,2592):
out = im.resize((792,1188), Image.ANTIALIAS), dpi=dpi)

Now when I try to save this jpeg as a PDF, specifying the dpi doesn't seem to make any diffrence, and I get an image that is larger than my original that looks like it has a lower dpi. Is there a way to mantain a consistent resolution when converting from Jpeg to PDF using PIL? Or is there a better way for me to go about doing this?

This is what I have for converting a directory of files from jpeg to pdf:

for infile in listing:

outfile = destpath + os.path.splitext(infile)[0] + ".pdf"
current = path + infile

if infile != outfile:
im =['dpi'], "PDF", Quality = 100)

Thanks for any help!

Answer Source

In the CHANGES file of PIL 1.1.7 sources one can read:

  • Added resolution save option for PDF files.

    Andreas Kostyrka writes: I've included a patched
    based on 1.1.6 as included in Ubuntu, that supports a "resolution"
    save option. Not great, but it makes the PDF saving more useful by
    allowing PDFs that are not exactly 72dpi.

So you should be able to do:, "PDF", resolution=100.0)

(seems to work fine on my Ubuntu box).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download