Image orientation and color information with PIL?

T

tvmaly

Does anyone know if it is possible to determine if an image is
horizontal/vertical and color or black & white using the python image
library? I have been searching this news group and the information was
not all clear on this.

Best Regards

Ty
 
I

Ivan Van Laningham

Hi All--

Does anyone know if it is possible to determine if an image is
horizontal/vertical and color or black & white using the python image
library? I have been searching this news group and the information was
not all clear on this.

How are you going to determine the orientation of an image without
sophisticated image analysis? I suspect Adobe Photoshop can do it, but
I don't know for sure. You'd have to look for things like sky, or
clouds, overcast sky, people's faces, and so on and so forth. It'd be
cool to have this available in Python, but unless the F-bot is busier
than I thought and working behind the scenes using his time machine,
it's not there now.

If you write it I'll use it;-)

Color vs B&W ought to be easy, though, by analysing the color table, if
there is one, and/or image mode. Check the PIL documentation. If you
have only searched the newsgroup then you might have overlooked the
docs.

Metta,
Ivan
----------------------------------------------
Ivan Van Laningham
God N Locomotive Works
http://www.andi-holmes.com/
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
 
J

Jeff Epler

i = Image.open("blue.jpg")'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...?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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFC2+CXJd01MZaTXX0RApN5AJ4+e+YZQ3S0y5KONR9g1gpIngkVtQCeLRPI
F//9CRC8z+igsVcTYyp8qZs=
=e3lg
-----END PGP SIGNATURE-----
 
S

Simon Dahlbacka

if you mean that you want to figure out which way the image is
depending on the actual data in the image, then you'll most likely get
to do the image processing yourself, on the other hand, if you are
talking jpegs from a relatively new camera then I suppose that you
should be able to get that info by looking at the EXIF information.
AFAIK you can read exif with PIL, but not write.
 
J

Jeff Epler

How are you going to determine the orientation of an image without
sophisticated image analysis? There is research on automatic image
orientation detection. [...]
If you write it I'll use it;-)

There's research going on in this area. Here are a few papers:
http://www.dcs.shef.ac.uk/teaching/eproj/msc2004/abs/m3zs2.htm
http://research.microsoft.com/research/pubs/view.aspx?pubid=918
there are probably many others.

I considered implementing one of these algorithms back in 2003 or so,
but instead I bought a digital camera with an orientation sensor.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFC2+FLJd01MZaTXX0RAoB/AKCodOJCroYciziq7xHzSfwthaR3rACdEUqp
QfrvksnD6fQC5h0dVqQUTys=
=0SYI
-----END PGP SIGNATURE-----
 
T

tvmaly

Jeff, this was exactly what I was looking for. I wrote a script with
this code and it worked perfectly.

Thanks

Ty
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top