Handle EXIF data with ruby (and style…)

March 9, 2008 by Michael

I use rmagick when it comes to deal with images. I’m not that disappointed like Zed Shaw with rmagick (but Zed is totally right when he points out on page 4: “[…] and is difficult to install (unless your computer is nearly exactly the same as the author’s).”), but when it comes to deal with EXIF, rmagick sucks big time.

After some searching i found another EXIF reader called “EXIF Reader” (EXIFR), which can be downloaded and installed from here or via gems.

I came mainly to this point becaus rmagick completly failed reading GPS data from my images (and it is hard to use with all other values).

EXIFR on the contrast cannot do image manipulation, so i needed both. Maybe i’m still just to new to ruby, but exifr gave me a hard time not reading the same image twice from file, once for rmagick and once for exifr.

I came up with the following solution:

require 'rubygems'
require 'RMagick'
require 'exifr'
image = Magick::ImageList.new("whatever_image.jpg")
exif_info = case image.format 
when 'JPEG'
  EXIFR::JPEG.new(StringIO.new(image.to_blob))
when 'TIFF'
  EXIFR::TIFF.new(StringIO.new(image.to_blob))
else
  nil
end

This uses rmagick to instantiate a magick image and then a StringIO object to build up the exif information if the image is a jpeg or a tiff image.

4 comments

  1. Tim Hunter wrote:

    I’m sorry you found RMagick’s EXIF support to be deficient. If you want to open a bug track on RubyForge I’ll be glad to investigate. If you decide to do this, please attach an example image to the track.

    Posted on March 25, 2008 at 1:29 AM | Permalink
  2. Michael wrote:

    Hey Tim,
    nice seeing visiting you. As i already mailed:

    I’m really sorry if my post was / is to harsh, but as i had these problems i remembered all the funny problems i had installing rmagick on OS X and so i quoted Zed.

    As i wrote, i really do like your rmagick, it does a great job at my http://dailyfratze.de site, but it couldn’t – at least for me – handle all exif fields.

    First thing for example, just getting the DateTimeOriginal of an image using rmagick:

    exifdata = image.get_exif_by_entry('DateTimeOriginal') unless image == nil
    datestring = exifdata[0][1] unless exifdata == nil || exifdata.length < 1

    I don’t know if i was using it wrong, but the same thing with exifr:

    date = (exif_data ? (exif_data.date_time_original || exif_data.date_time) : nil)

    I’m content having to specialized things to do their jobs.

    The fiels not being read by exif (at least, not in a usable way for me) were GPS:Latitude and GPS:Longitude. I don’t have the code anymore but somewhere i got a datastructure of these fields but was able to get my hands on the values. In the end, i found this http://www.ruby-forum.com/topic/70422 post via google and tried the solution suggested there and added my little code above for not being forced to read an image file twice, for processing it with rmagick and exifr.

    Have a great day and thanks for visiting my little blog :),

    Michael

    Posted on March 25, 2008 at 7:32 AM | Permalink
  3. eller82 wrote:

    Auch wenn dein Post schon etwas älter ist, so hat er mir doch heute noch geholfen. EXIFR ist zwar ein wenig komisch, aber zum GPS auslesen super!

    Posted on February 16, 2013 at 12:29 PM | Permalink
  4. Michael wrote:

    André: Gerne! Freut mich. So ist df.v1 im Nachhinein noch zu was gut 🙂

    Kannst mir auch immer gerne konkrete Fragen stellen.

    Posted on February 16, 2013 at 2:53 PM | Permalink
One Trackback/Pingback
  1. Quo vadis, Planet-Punk.de? | Planet-Punk.de on March 27, 2008 at 4:31 PM

    […] zu treten, dass sie Antworten auf meine Posts geschrieben haben, so wie es mir mit Grails und RMagick gelungen […]

Post a Comment

Your email is never published. We need your name and email address only for verifying a legitimate comment. For more information, a copy of your saved data or a request to delete any data under this address, please send a short notice to michael@simons.ac from the address you used to comment on this entry.
By entering and submitting a comment, wether with or without name or email address, you'll agree that all data you have entered including your IP address will be checked and stored for a limited time by Automattic Inc., 60 29th Street #343, San Francisco, CA 94110-4929, USA. only for the purpose of avoiding spam. You can deny further storage of your data by sending an email to support@wordpress.com, with subject “Deletion of Data stored by Akismet”.
Required fields are marked *