[Image-SIG] Image orientation and color information with PIL?

Jeff Epler jepler at unpythonic.net
Mon Jul 18 19:02:15 CEST 2005


>>> i = Image.open("blue.jpg")
>>> i.size
(3008, 2000)
>>> i.mode
'RGB'

'RGB' is the value for color jpeg images.  I believe that for black&white
images, i.mode is 'L' (luminosity).

If you want to determine whether an existing image is landscape or portrait,
then just compare i.size[0] (width) and i.size[1] (height).

If by "determine if an image is horizontal/vertical", you want to find
the orientation data recorded by some digital cameras, you can do that
with PIL 1.1.4.  According to the release notes for 1.1.4,
    + Added experimental EXIF support for JPEG files.  To extract EXIF
      information from a JPEG file, open the file as usual, and call the
      "_getexif" method.  If successful, this method returns a dictionary
      mapping EXIF TIFF tags to values.  If the file does not contain EXIF
      data, the "_getexif" method returns None.

      The "ExifTags" module contains a dictionary mapping tags to tag
      names.

      This interface will most likely change in future versions.

The exif tag 274 is Orientation.  The values you'll see most often are 1
(Normal), 6 and 8 (90 and 270 degree rotations).  Orientation can also encode
180 degree rotation, as well as any of the four rotations combined with a
mirror operation.

>>> [k for (k,v) in ExifTags.TAGS.items() if v == 'Orientation']
[274]
>>> e = i._getexif()
>>> if e: print e[274]
1

I have written a standalone Python module that reads and changes the EXIF orientation data.  You can view it here:
    http://unpy.net/cgi-bin/viewcvs.cgi/aethertool/disorient.py?rev=1.2&content-type=text/vnd.viewcvs-markup
It is available under the terms of the GNU GPL.

Here's another page about EXIF orientation data:
    http://sylvana.net/jpegcrop/exif_orientation.html

Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mail.python.org/pipermail/image-sig/attachments/20050718/d62084de/attachment.pgp


More information about the Image-SIG mailing list