Is it possible to change a picture resolution with Python?

Tim Roberts timr at probo.com
Thu Sep 21 02:36:16 EDT 2006


"Lad" <python at hope.cz> wrote:

>from
>> image:
>> http://www.pythonware.com/library/pil/handbook/image.htm
>>
>> This is some example code:
>>
>> from PIL import Image
>> im = Image.open("1.jpg")
>> nx, ny = im.size
>> im2 = im.resize((int(nx*1.5), int(ny*1.5)), Image.BICUBIC)
>> im2.save("2.png")
>>
>> Bye,
> bearophile,
>Thank you for your reply.
>But the above code increases size only , but not DPI resolutions(
>vertical nad horizontal).I need  a higher  vertical and horisontal
>resolutions.

I'm not convinced that you know what you are asking for.

Let's say you have an image that is 300x300 pixels, that is tagged as being
100 dpi.  Such an image is expected to be printed at a 3" x 3" size.

Now, if you want to increase the dpi to 300 dpi, what does that mean?  Does
it mean you want that same picture to be printed at a 1" x 1" size?  If so,
then all you need to change is the picture header.  It will still be
300x300 pixels, and the file will be the same number of bytes.

Or, do you mean that you want the picture to continue to print as 3" x 3",
but to have three times as many pixels in each direction?  That means you
have to increase the number of pixels to 900x900.  That's exactly what the
code above is doing.  The image file will be 9 times larger.
-- 
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list