# How to extract a specific element from EXIF photographic metadata?

It's now possible to import photographs into Mathematica and import the EXIF data at the same time:

i = Import["ExampleData/coneflower.jpg", "ImageWithExif"]


You can now look at all the metadata:

Options[i, MetaInformation]


{MetaInformation -> {"Exif" -> {"ImageDescription" -> " ", "Make" -> "NIKON", "Model" -> "E950", "Orientation" -> 1, "XResolution" -> 300, "YResolution" -> 300, "ResolutionUnit" -> 2, "Software" -> "Adobe Photoshop CS3 Macintosh", "DateTime" -> "2008:08:19 11:29:05", etc...

So how would I extract - say - the exposure time ("ExposureTime")? I was trying something like this:

"ExposureTime" /. Options[i, MetaInformation] ...


but I don't know enough about the structure of the stored metainformation. Can you do this without knowing that structure?

-

Does this do want you want?

Cases[Options[i, MetaInformation],
HoldPattern["ExposureTime" -> ___], Infinity]


or even simpler because it is Rules all the way down:

Cases[Options[i], HoldPattern["ExposureTime" -> ___], Infinity]

{"ExposureTime" -> 1/65}


All-in-one:

With[{wanted = "ExposureTime"},
wanted /. Cases[Options[i], HoldPattern[wanted -> ___], Infinity]] //
N

-
Thanks, yes, that seems to work... So N[Last[Last[ Cases[Options[f], HoldPattern["ExposureTime" -> ___], Infinity]]]] is what I need?! Looks a tad clunky... – cormullion Dec 5 '12 at 15:22
@cormullion I´d go for a something like With[{wanted = "ExposureTime"}, wanted /. Cases[Options[i], HoldPattern[wanted -> ___], Infinity] ] // N – Yves Klett Dec 5 '12 at 15:39
@cormullion but yes, there ought to be something cleaner - let´s wait for better approaches – Yves Klett Dec 5 '12 at 15:48

Here is something based on FilterRules and using your post of Exif metadata format.

FilterRules

FilterRules["Exif" /. (MetaInformation /. Options[i, MetaInformation]), "XResolution"]


{"XResolution" -> 300}

There may be more elegant ways to extract the rules which form part of MetaInformation, I await them with interest.

This unpacks nested sets of rules:

FilterRules[ Options[i, MetaInformation] //. {_ -> z_} -> z, "XResolution"]


{"XResolution" -> 300}

OptionValue

Here is an interesting alternative which does require some knowledge of the structure of the MetaInformation but is otherwise nicely concise.

OptionValue[Options[i, MetaInformation], MetaInformation -> "Exif" -> "XResolution"]


300

-
+1 ...but you need more information on the structure of MetaInformation... – Yves Klett Dec 5 '12 at 16:24
@YvesKlett Fair point :) I've had a go at finding so something less dependent on the form of MetaInformation. – image_doctor Dec 5 '12 at 16:41
Uhhh... wicked ;-) – Yves Klett Dec 5 '12 at 17:28
Excellent - getting concise-r, but still it's not as slick as I would have expected from Mathematica... :) – cormullion Dec 5 '12 at 19:19
@cormullion Yes, I get what you are saying :) – image_doctor Dec 5 '12 at 21:59