Can I extract multiple EXIF tags in one go?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Richard-E
Posts: 3
Joined: 2016-03-05T14:59:42-07:00
Authentication code: 1151

Can I extract multiple EXIF tags in one go?

Post by Richard-E »

Hello,

I"m trying to build a catalogue of around 15,000 image files currently stored in a folder structure on a linux (Ubuntu) server.

The plan is to write a shell script that will loop through the folder structure and store the file name/path & selected EXIF information about each image in a SQL database (one record per image).

I was planning to use ...

Code: Select all

identify -format %[EXIF:tagName] file.jpg
... to extract the EXIF info.

However, I think I need to use this command separately for each piece of EXIF data that I'm interested in (e.g. created date, FNumber, ExposureTime, Make, Model, etc) and this repetition is making the script very slow.

Is there a way that I can extract list of EXIF tags in one go, i.e. calling identify just once per image? I see there's an option to print all the tags using %[EXIF:*], but I don't want all the tags, just a subset.

Any suggestions would be appreciated.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Can I extract multiple EXIF tags in one go?

Post by fmw42 »

Code: Select all

identify -format "%[EXIF:tagName1] %[EXIF:tagName2] ... %[EXIF:tagNameX]\n" file.jpg
Enclose them all in the double quotes as above
Richard-E
Posts: 3
Joined: 2016-03-05T14:59:42-07:00
Authentication code: 1151

Re: Can I extract multiple EXIF tags in one go?

Post by Richard-E »

Ah, thanks. That seems to work well. I'm grateful to you.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Can I extract multiple EXIF tags in one go?

Post by GeeMack »

Richard-E wrote:Ah, thanks. That seems to work well. I'm grateful to you.
You got the definitive answer from fmw42 already. You might be able to build on that idea to create a comma separated list you can (probably) easily import directly into your database program.

Code: Select all

identify -format "%f,%[EXIF:tagname_1],%[EXIF:tagname_2],%[EXIF:tagname_more], ... \n" *.jpg > image_data.csv
That would run through all the JPG files in the directory and use their data to create a multi-line file "image_data.csv". Each line would start with the image file name "%f", followed by the values contained in your EXIF tags, and all separated by commas. Then you should be able to import that file directly into your SQL database with a bulk insert. (You're on your own for the specific method.)

Some EXIF tags contain commas. If any of the tags you're accessing have commas in the tag, you'll need to use some other character as the delimiter, maybe a pipe "|" or semicolon ";".

IM provides a way to access other pieces of information you can add to that "-format ..." string. You can add them by using percent escapes like the "%f" for the file name I used in my example above. Most of those attributes can be found on the web page at this link.

There may be some number of JPG files in the directory and/or the total sizes of those files that makes the "identify" program choke, depending the amount of memory in your system.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Can I extract multiple EXIF tags in one go?

Post by fmw42 »

You could put your custom identify command into a PHP exec() command and from within PHP transfer the data to your SQL database.
Richard-E
Posts: 3
Joined: 2016-03-05T14:59:42-07:00
Authentication code: 1151

Re: Can I extract multiple EXIF tags in one go?

Post by Richard-E »

Thanks again both. I have a bit more work to do on this but I think I'm heading for a solution that generates a comma-separated list of values, pretty much as you suggest, but which I can then use immediately in SQL INSERT statement. The gist of it - I think - will be (bash) shell script something like ...

Code: Select all

EXIF_values=$(identify -format "'%[EXIF:make]','%[EXIF:model]','%[EXIF:FNumber]',%[EXIF:ExposureTime],%[EXIF:ISOSpeedRatings],'%[EXIF:DateTimeOriginal]'" $item)

INSERT INTO table_photos (make, model, Aperture, etc ...) VALUES ($EXIF_values) 
(where $item is the current file at each step in the loop). The string $EXIF_values should expand into the list that the SQL command needs - so long as I make sure I have them in the right order! SQL does need some of the values enclosed in quotes, but that doesn't seem to be an issue as the IM command happily allows me to insert single quotes.

Thanks for the tip about other %-escapes. I'll be using some of them although probably not filename because many of those in this case do contain commas (this is my main reason at the moment for not going for the csv approach).

I had thought about using a PHP script for this, and may still do if it gets much more complicated. I need to error trap stray non-image files that might have found their way into the main folder structure (easy enough), and there are quite a lot of images in the set that are scanned 35mm negatives, so won't have some of the EXIF data that I'll be storing for digital camera images. I think the approach above will cope with those, but I need to test some permutations. It looks like IM throws an error message like 'identify: unknown image property "%[EXIF:FNumber]" @ warning/property.c/InterpretImageProperties/3597.' for the scanner files, but the $EXIF_data string that I'm generating seems just to contain empty strings between commas, so I think I'll be able to use it. If not, then the string does seem to contain the scanner make/model, so I can test for that and generate a modified $EXIF_values string if necessary.

Many thanks again.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Can I extract multiple EXIF tags in one go?

Post by fmw42 »

If you want to avoid "warnings", add -quiet to your identify command. See http://www.imagemagick.org/Usage/basics/#controls
Post Reply