change dpi without changing pixels dimensions with PIL?

Fredrik Lundh fredrik at pythonware.com
Wed Dec 17 22:46:22 CET 2003


"duikboot" <a at spam.nl> wrote:

> In Photoshop I can, when I want to change the dpi(dots per inch) of a
> picture, arrange that the picture won't be resized. Do you know of a way, I
> can accomplish this in python with PIL? If I use this script it changes the
> dpi, but the picture becomes huge.
>
> Thanks
>
> Arjen
>
> import sys
> import Image
> import os
> a=sys.argv[1]
> print a
> c="or"+a
> b=os.system("rename %s %s" % (a, c))

That's usually spelled os.rename(a, c)

> im=Image.open(c)
> print im.info["dpi"]
> im.save(a, dpi=(150, 150))

I think you're confusing image size with file size.

"Image.open" loads the image into a PIL Image object, and "save"
creates a new file from the contents of that object.  The image size
(in pixels) doesn't change when you do this.

However, the file size may change.  For some file formats, PIL can
read compressed files but not write files using the same compression
(usually for legal reasons).  GIF is one such example.  TIFF LZW is
another one.

If you can, use PNG instead.

</F>








More information about the Python-list mailing list