Reading out EXIF data via Python

Actually I thought to do it in PHP, but for security issues and also for improving my Python skills I decided to do it in Python. The first problem already was finding a module that can read out the EXIF data of images. After trying pexif I was a bit disappointed, it just couldn’t read my pic’s data. There were some tags it couldn’t handle, so it just stopped with an exception. Then I was going to try jpeg a simple module, there I read in the docs that for doing this and that you should use http://pyexif.sourceforge.net - ok, now I know what I need. Actually it was clear that the module would be named pyexif, but for some reason I was not searching for it. So I downloaded it and thanks to sourceforge this didn’t take less than about half an hour. After unpacking and skimming through the files I was astonished that there was no setup.py. Ok, back to their project page and read a bit again. Oh great, the last comment in big red letters (from 2002) said that they hadn’t done anything on the library for a long time, f***. And I should go and check out “Gene Cash’s EXIF.py library”, which I finally found here. To judge by the comments this one has also been last updated in 2004. Oh man, I think I am going back to PHP, there will surely be up-to-date libraries. But I will give it just another chance, let’s see if there are going to be any interesting hints and/or comments to this entry, hopefully about new modules I didn’t find. Disappointed …

18 Comments »

  1. Stan Seibert said,

    March 31, 2006 at 1:18 am

    I don’t know if you are totally free to pick your language, but ExifTool is a perl program that handles everything I’ve seen stuck in the EXIF data, included manufacturer specific attributes, like the ISO speed on Nikon dSLR photos.

  2. Justin said,

    March 31, 2006 at 2:37 am

    This isn’t good enough?

    from PIL import Image
    from PIL.ExifTags import TAGS

    def get_exif(fn):
    ret = {}
    i = Image.open(fn)
    info = i._getexif()
    for tag, value in info.items():
    decoded = TAGS.get(tag, tag)
    ret[decoded] = value
    return ret

  3. Wolfram said,

    March 31, 2006 at 10:22 am

    Justin, thanks. That’s seems like the ultimate tool I was looking for.
    http://www.pythonware.com/products/pil/
    I just can’t find how to open a file from a URL, do I really have to download it first doing:
    fp = file(filename, “wrb”)
    fp.write(urlopen(pic).read())
    and then use it from the disk, or did I just not look hard enough?

  4. matt said,

    March 31, 2006 at 10:35 am

    I don’t understand your issue with EXIF.py? It appears that it is because no one has touched the code in a year or so? It appears to work fine on my machine…

    python /tmp/EXIF.py Desktop/CANON_DC/DCIM/175CANON/IMG_7568.JPG
    Desktop/CANON_DC/DCIM/175CANON/IMG_7568.JPG:
    EXIF ApertureValue (Ratio): 95/32
    EXIF ColorSpace (Short): 1
    EXIF ComponentsConfiguration (Undefined): YCbCr
    …..

    Maybe your issue is that it is too verbose? Should be really simple to filter the output, just ignore the keys that you don’t want to print…

  5. Pierre said,

    March 31, 2006 at 2:53 pm

    As I like your new passion for Python (I find myself hacking with it too), I do not understand the “security” argument here. Are you falling in the FUD of the year? PHP is insecure by default?

  6. Hamish said,

    March 31, 2006 at 3:20 pm

    I’ve wrapped exiv2 (a C++ EXIF and IPTC library) with SWIG. Eventually it’ll be on my site, but that’s non-existent for now (copious-free-time issues). ‘Cause it’s a wrapper, it’s platform dependent, but I’ve tested it on both Linux and win32. I only use it for a limited IPTC subset, so I’ve no idea if the EXIF type conversion is correct or not. It does support writing as well as reading though, and the underlying library is fairly complete.

    Get in touch if pure python isn’t a requirement.

  7. Wolfram said,

    March 31, 2006 at 3:24 pm

    Hi Pierre, it’s a good alibi isn’t it? Actually I just didn’t want to start with PHP for this app and just stick completely to Python ;-) the second reason is the actual one, you are right …

  8. Hamish said,

    March 31, 2006 at 4:06 pm

    Huh. Space for an email address, but it doesn’t show up in the comments. Anyway, you can grab the source at http://www.mindcontroldogs.com/exiv2py-0.1.zip or a win32 binary at http://www.mindcontroldogs.com/exiv2py-0.1.win32.zip.

  9. Jos Yule said,

    March 31, 2006 at 5:30 pm

    I just tried PIL’s Image.open when using a urllib.urlopen()’ed file. Unfortunately the file-like object urllib returns doesn’t have a ’seek’ method which Image.open requires.

    However, you can use urllib.urlretrieve(), which saves the object the url points to in a local file (see the docs). So you can do something like the following:

    ## CODE START

    import Image
    import urllib

    dl_img = urllib.urlretrieve(”URL-to-image”)
    img = Image.open(dl_img[0])

    ## CODE END

    dl_img is a tuple, the first part being the path to the locally saved file. Again, see the docs for urllib.

  10. Wolfram said,

    March 31, 2006 at 5:52 pm

    Jos, thanks a lot.

  11. Fredrik said,

    March 31, 2006 at 5:56 pm

    “Unfortunately the file-like object urllib returns doesn’t have a ’seek’ method which Image.open requires.”

    To avoid temporary files, you can load the data into a StringIO instance:

    import urllib, cStringIO

    file = cStringIO.StringIO(urllib.urlopen(URL).read())
    img = Image.open(file)

    A more advanced approach is to use the ImageFile.Parser class, and “feed” incoming data directly into the parser object (see the documentation for an example).

    Hope this helps! /F

  12. Jos Yule said,

    March 31, 2006 at 6:25 pm

    Nice one - i’ll take a look at the Parser class too.

  13. Kevin Horton said,

    April 3, 2006 at 2:23 pm

    This comment has nothing to do about EXIF parsing, but I couldn’t find another obvious way to get in contact with Wolfram.

    I wanted to pick up the RSS feed for this blog, but found that the two feeds listed (Entries and Comments), are identical. They both actually contain only the comments. This looks like a blunder, and I figured you might want to know about it.

  14. Stephen Vermeulen said,

    April 3, 2006 at 8:19 pm

    You might also want to look at:

    http://www.emilas.com/jpeg/

  15. towi said,

    April 7, 2006 at 5:03 pm

    Hi,

    I use Photoshop to read nearly all EXIF data. I have a photoshopy.py module and use the COM+ typelib to remote control photoshop. If you are interested I can send you more detail. It works something like this:

    app = photoshopy.Application()
    doc = app.Load(”MyImg.jpg”)
    print doc.EXIF[u"Author"]
    print doc.EXIF[u"Aparture"]

    I don’t have the script right here, but its quite easy…

    tschau, towi.

  16. Wolfram said,

    April 10, 2006 at 1:16 pm

    btw Pierre, actually it’s a little bit the same as it was a couple years ago with Mac OS and Windows, I switched to Mac OS because I wanted less trouble with the computer caused by viruses etc. (a computer causes enough trouble besides, so avoid those at least).
    Now with PHP and Python, actually the feeling was the same, just use the (less common) language that has less _known_ issues, just to ship around those problems by default. Yes, that was one of my main reasons why to switch to Python … that it didn’t quite work out yet is written on another piece of paper …

  17. Nare said,

    July 11, 2008 at 10:17 am

    Hi Towi,

    Can u provide me \"photoshopy.py\"

  18. Bob Wiliiams said,

    March 18, 2009 at 6:36 pm

    Personally I like EXIF.py with is totally self contained and works well. It’s easy to use. It is a sourceforge project, but the official release didn’t work for me and I used a bug-fix version I found here:

    http://www.dudek.org/blog/154

    I think the bug-fix version is mostly needed of the unusual EXIF formatting you get from some versions of iphoto.

RSS feed for comments on this post · TrackBack URL

Leave a Comment