[Image-SIG] Setting DPI field when saving a PNG file

Fredrik Lundh fredrik at pythonware.com
Thu May 7 03:17:47 CEST 2009


On Wed, May 6, 2009 at 11:11 PM, Dave Lajoie <dave.lajoie at autodesk.com> wrote:
> Hello Everyone!
> I am attempting to save a PNG file, where the source file doesn't have dpi fields.
> What is the proper way to enforce a new DPI with .save() function?
>
> I have attempted to modify the .info dictionary by adding a dpi entry, but it never gets saved for some reason.
> I am missing something really obvious here. :)

PIL doesn't preserve the contents of the info attribute when you do
image operations, since it's pretty much impossible to know how the
change affects the contents of the dictionary.  All you can use it for
is to access information provided by the file reader, directly after
open.

> import Image
> print "Loading Demo.png"
> im = Image.open("C:\\Demo.png")
> print "Demo.png Info:\n"
> print im.info
> print "________________________"
> print "Changing Demo.png dpi to 72"
> im.info["dpi"]=(72,72)
> print "________________________"
> print "Demo.png Info:\n"
> print im.info
> im.save("C:\\Demo_dpi.png")

try:

im.save(filename, dpi=(72, 72))

</F>


More information about the Image-SIG mailing list