Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image Library convert from JPEG to

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.open("Image.jpg")

dpi=im.info['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)

out.save(project, dpi=dpi)

Now when I try to save this JPEG as a PDF, specifying the dpi doesn't seem to make any difference, 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 = Image.open(current)
        dpi=im.info['dpi']

        im.save(outfile, "PDF", Quality = 100)
like image 803
AlexGilvarry Avatar asked Sep 27 '12 17:09

AlexGilvarry


People also ask

How to convert JPEG to JPG?

How to Convert JPEG to JPG? Click the “Choose Files” button to select your JPEG files. Click the “Convert to JPG” button to start the conversion. We use both open source and custom software to make sure our conversions are of the highest quality.

How to convert JPG to SVG for free?

The free JPG to SVG converter tool from Adobe Express lets you upload your JPG image and convert it to a vector in seconds. How to convert a JPG to SVG. 1. Select. Choose a JPG image from your photo library that is less than 2GB in size.

What is the use of a JPG file?

JPG extension was assigned to the image files. Many photos and web graphics are saved in JPG. In order to compress many bitmaps are saved in .jpg, that makes it easier to transfer and download these files on the Internet.

How do I convert JPG to PDF on Mac?

Upload your image to the JPG to PDF converter. Adjust the letter size, orientation, and margin as you wish. Click ‘Create PDF now!’ and wait for the conversion to take place. And that's all there is.


1 Answers

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 PdfImagePlugin.py
    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:

im.save(outfile, "PDF", resolution=100.0)

(seems to work fine on my Ubuntu box).

like image 93
Vicent Avatar answered Sep 28 '22 05:09

Vicent