AlexGilvarry
AlexGilvarry

Reputation: 165

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!

Upvotes: 6

Views: 11328

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121744

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)

Upvotes: 8

Related Questions