Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Image Library Image Resolution when Resizing

I am trying to shrink some jpeg images from 24X36 inches to 11X16.5 inches using the python image library. Since PIL deals in pixels this should mean scaling from 7200X 4800 pixels to 3300 X2200 pixels, with my resolution set at 200 pixels/inch, however when I run my script PIL changes the resolution to 72 pixels/inch and I end up with a larger image than i had before.

import Image

im = Image.open("image.jpg")

if im.size == (7200, 4800):
    out = im.resize((3300,2200), Image.ANTIALIAS)
elif im.size == (4800,7200):
    out = im.resize((2200,3300), Image.ANTIALIAS)

out.show()

Is there a way to mantain my image resolution when I'm resizing my images?

thanks for any help!

like image 921
AlexGilvarry Avatar asked Sep 26 '12 19:09

AlexGilvarry


People also ask

How do I resize an image but keep quality?

If you want to resize an image without losing quality, you need to make sure that the "Resample" checkbox is unchecked. This checkbox tells Paint to change the number of pixels in the image. When you uncheck this box, Paint will not change the number of pixels, and the quality of the image will not be reduced.

Do images lose quality when resized?

Most of the time, reducing an image's size or dimensions won't affect the image's quality. Making an image to be larger than its original dimensions can be tricky. Resizing an image larger than its original dimensions can affect the quality.


1 Answers

To preserve the DPI, you need to specify it when saving; the info attribute is not always preserved across image manipulations:

dpi = im.info['dpi']  # Warning, throws KeyError if no DPI was set to begin with

# resize, etc.

out.save("out.jpg", dpi=dpi)
like image 75
Martijn Pieters Avatar answered Sep 18 '22 01:09

Martijn Pieters