2

I am attempting to pull the EXIF information for images that have values for the EXIF "keywords" attribute. I have successfully read EXIF information using mini_magick by simply opening an image and using:

image["EXIF:Model"]

For some reason, none of the following will return keywords for an image that I know has them:

image["EXIF:Keywords"]
image["EXIF:XPKeywords"]
image["EXIF:Subject"]

I have confirmed that the image in question does have this information using this utility: http://regex.info/exif.cgi

Does mini_magick only have access to certain EXIF values? It seems so odd that I can access certain EXIF information but not others.

Nash Bridges
  • 2,350
  • 14
  • 19
retailevolved
  • 485
  • 2
  • 16

2 Answers2

2

EXIF metadata is created by camera, therefore it contains only technical related stuff. What you actually want to access is IPTC and XMP.

Imagemagick, which is behind mini_magick, allows to read IPTC, e.g. image["%[IPTC:2:25]"] for keywords (update: be aware of perfomance issues, see comments).

As for XMP, I don't know an easy way to do this. You can try to run

`identify -verbose #{your_filename}`

and then grub lines that include xmp.

Nash Bridges
  • 2,350
  • 14
  • 19
  • Thanks for the links. Very useful. However when using mini_magick, the command image["IPTC:2:25"] simply returns "IPTC:2:25" - I don't think that mini_magick supports accessing IPTC data as it does EXIF. I did however find a workaround for this situation that I will post in an answer. – retailevolved Apr 01 '12 at 11:14
  • @retailevolved You're right, mini_magick [doesn't support IPTC](https://github.com/probablycorey/mini_magick/blob/master/lib/mini_magick.rb#L204) yet. I was sure that they had implemented all functionality around Imagemagick, but I was wrong. – Nash Bridges Apr 01 '12 at 12:04
  • @retailevolved I found a way to retreive IPTC, see my updated answer. – Nash Bridges Apr 01 '12 at 12:42
  • Aha! I hated having to use two extra gems for this. Felt wrong. Great find. – retailevolved Apr 01 '12 at 17:08
  • I did some performance testing. I used the MiniMagick method versus the exifr / xmp gem method. The results were astounding. I looped through 100 times of opening a 10 MB jpeg from my local hard drive and outputting keywords. ImageMagick took nearly 2 minutes to do this. exifr / xmp took 3 seconds. exifr must not have to read the entire image or something. It is FAST. Guess I'll stick with that one for now. If I was in a situation where I already had the image loaded in MiniMagick, I would use the method you discovered. – retailevolved Apr 01 '12 at 17:35
1

As Nash Bridges pointed out, I needed to actually access XMP data. I found a way to do this using two gems: exifr and xmp. After installing both of those gems, I used the following code in my controller that processes the image upload:

tags = XMP.parse(EXIFR::JPEG.new(StringIO.new(params[:file].read))).dc.subject.join(',').downcase

I was unable to find a way to do this with only mini_magick though I'm sure that it would be possible by parsing the raw data output by the "to_blob" method of a mini_magick Image model.

retailevolved
  • 485
  • 2
  • 16