Skip to content
accelerando

Handle EXIF data with ruby (and style…)

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 25-Mar-08 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 25-Mar-08 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 16-Feb-13 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 16-Feb-13 at 2:53 pm | Permalink

One Trackback/Pingback

  1. [...] 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 nor shared. Required fields are marked *
*
*

*