Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Image orientation and color information with PIL?

Reply
Thread Tools

Image orientation and color information with PIL?

 
 
tvmaly@gmail.com
Guest
Posts: n/a
 
      07-18-2005
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

 
Reply With Quote
 
 
 
 
Ivan Van Laningham
Guest
Posts: n/a
 
      07-18-2005
Hi All--

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
> 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/worksh...oceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
 
Reply With Quote
 
 
 
 
Jeff Epler
Guest
Posts: n/a
 
      07-18-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/...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+YZQ3S0y5KONR9g1g pIngkVtQCeLRPI
F//9CRC8z+igsVcTYyp8qZs=
=e3lg
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Simon Dahlbacka
Guest
Posts: n/a
 
      07-18-2005
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.

 
Reply With Quote
 
Jeff Epler
Guest
Posts: n/a
 
      07-18-2005
On Mon, Jul 18, 2005 at 10:55:42AM -0600, Ivan Van Laningham wrote:
> 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/e.../abs/m3zs2.htm
http://research.microsoft.com/resear...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-----

 
Reply With Quote
 
tvmaly@gmail.com
Guest
Posts: n/a
 
      07-20-2005
Jeff, this was exactly what I was looking for. I wrote a script with
this code and it worked perfectly.

Thanks

Ty

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing font color from current font color to black color Kamaljeet Saini Ruby 0 02-13-2009 04:58 PM
Image orientation changes jibbajibba HTML 14 08-31-2007 06:56 PM
getting pixel color information from an image rfractal30 Java 5 03-06-2005 09:20 PM
MS-WORD 97 (and 2000) Envelope and Label Orientation Jeff Wisnia Computer Support 2 06-04-2004 11:38 PM
<FORM METHOD="post" onSubmit="return fieldcheck()" name="orientation" action="http://ws-kitty.BU.edu/AT/survey/orientation/script/write.asp" language="JavaScript"> Joeyej ASP .Net 0 06-04-2004 07:55 PM



Advertisments