Image orientation and color information with PIL?

Discussion in 'Python' started by tvmaly@gmail.com, Jul 18, 2005.

  1. Guest

    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
     
    , Jul 18, 2005
    #1
    1. Advertising

  2. Hi All--

    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/workshops/1998-11/proceedings.html
    Army Signal Corps: Cu Chi, Class of '70
    Author: Teach Yourself Python in 24 Hours
     
    Ivan Van Laningham, Jul 18, 2005
    #2
    1. Advertising

  3. Jeff Epler Guest

    >>> 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...?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-----
     
    Jeff Epler, Jul 18, 2005
    #3
  4. 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.
     
    Simon Dahlbacka, Jul 18, 2005
    #4
  5. Jeff Epler Guest

    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/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-----
     
    Jeff Epler, Jul 18, 2005
    #5
  6. Guest

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

    Thanks

    Ty
     
    , Jul 20, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rfractal30
    Replies:
    5
    Views:
    6,974
    rfractal30
    Mar 6, 2005
  2. Daniel Mark
    Replies:
    1
    Views:
    786
    Fredrik Lundh
    Sep 25, 2006
  3. jibbajibba

    Image orientation changes

    jibbajibba, Aug 29, 2007, in forum: HTML
    Replies:
    14
    Views:
    1,442
    dorayme
    Aug 31, 2007
  4. Sverre
    Replies:
    2
    Views:
    3,694
    Sverre
    Dec 17, 2009
  5. Kamaljeet Saini
    Replies:
    0
    Views:
    494
    Kamaljeet Saini
    Feb 13, 2009
Loading...

Share This Page