问题
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.
回答1:
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
.
回答2:
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.
来源:https://stackoverflow.com/questions/9960551/how-to-get-exif-keywords-using-mini-magick-in-a-rails-app