Image orientation and color information with PIL?
jepler at unpythonic.net
Mon Jul 18 19:02:15 CEST 2005
>>> i = Image.open("blue.jpg")
'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 (width) and i.size (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
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
>>> [k for (k,v) in ExifTags.TAGS.items() if v == 'Orientation']
>>> e = i._getexif()
>>> if e: print e
I have written a standalone Python module that reads and changes the EXIF orientation data. You can view it here:
It is available under the terms of the GNU GPL.
Here's another page about EXIF orientation data:
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 196 bytes
Desc: not available
More information about the Python-list