[Image-SIG] File Resolution saving....

kcaza@cymbolic.com kcaza@cymbolic.com
Sun, 17 Sep 2000 12:28:32 -0700


A while back, I posted a message about how to save image resolution into the
file.  Since then, I've managed to 'hack' support for this into TIFF, EPS, PDF,
PNG, and BMP file formats.  Most of these are pretty straight forward, as this
function is supported by the file format, but 'neglected' (or set to a default
value) by PIL.

The code below shows the addition to TiffImagePlugin that I had to make, and
I'll forward the other hacks to Fred if he's supportive of adding this in the
future.  I find this a VERY necessary feature so that images are sized properly
when printed by the user.  As it is, everything usually defaults to 72
pixels/inch, and it's up to the user to manually resize images when they're
printing them (if their printing application even supports this).

I am having difficulty adding support for this to JPEG... I've looked at the IJG
library, and it supports it with "density_unit", "X_density" and "Y_density",
however I'm really not having any luck successfully passing this info to the
encoder.  Any help there would be appreciated!

Thanks,

Kevin Cazabon
kcazabon@home.com

# add this to line 568 of TiffImagePlugin.py

    #################################
    # HACK to let the user specify X and Y Resolution Tags for saving
    # simply create an im.RES or (im.XRES, im.YRES) attribute, with the
    # pixels per inch that you wish before saving
    #
    # alternatively, if your image was loaded from a TIFF file, it will
    # preserve the original PPI of the file unless you specify an im.RES,
    # im.XRES, or im.YRES value.
    # by Kevin Cazabon, Sept 2000 kcazabon@home.com
    if ("XRES" in dir(im) and "YRES" in dir(im)) or "RES" in dir(im):
        if "RES" in dir(im):
            ifd[282] = int(im.RES + 0.5)           # Tag 282 is for XRESOLUTION
            ifd[283] = int(im.RES + 0.5)           # Tag 283 is for YRESOLUTION
        if "XRES" in dir(im):
            ifd[282] = int(im.XRES + 0.5)
        if "YRES" in dir(im):
            ifd[283] = int(im.YRES + 0.5)

    # Note that if you've done anything with the file after opening, the TIFF
tag directory is lost
    # so the following won't work.
    #you can save the im.tag attribute when opening it, and re-attach it before
saving if you like

    elif "tag" in dir(im):  #meaning that it was loaded as a TIFF file already
        if im.tag.tagdata.has_key(282):
            ifd[282] = im.tag.tagdata.get(282)
        if im.tag.tagdata.has_key(283):
            ifd[283] = im.tag.tagdata.get(283)

#################################

an example of usage would be:

import Image

# create a new image and specify the resolution
im = Image.new("RGB", (100,100))
im.RES = 300 # meaning 300 pixels per inch
im.save("c:/temp/test.tif")

# or load an existing TIFF and preserve the existing resolution when saving
im2 = Image.open("c:/temp/existing_400ppi_image.tif")
im2.save("c:/temp/still_400ppi.tif")