Sunday, March 07, 2010

Exif Woes

I have a local web script I use when uploading photos to Flickr so that all the information I add (e.g. title, description, tags etc.) is stored in the Exif of the photo, meaning if I ever move away from Flickr I still have all the extra meta-data I've added for each photo.

One of the things I wanted to do was tag which lens the photo was taken with, I had just been trying to work it out based on the focal length and aperature (e.g. if it's less than f/5.6 at certain focal lengths I know it's my Tamron 17-50mm f/2.8 rather than the Sony kit lens and anything over 70mm has to be the Tamron 55-200mm). This works to a point, but e.g. anything taken between 17mm and 50mm at f/5.6 or higher could be either the kit lens or the Tamron.

Neil had claimed that the lens information was stored in the Exif, but I wasn't convinced as I hadn't seen it for any of my photos, I wondered if maybe only the newer Sonys contained this information, but after checking on Flickr, sure enough there it is "Lens Type: Tamron Lens (255)". I'd assumed I'd just missed this, so yesterday when I came to upload another photo I thought I'd give it a try and update my process.

The thing is, that the "Lens Type" field isn't one of the standard fields returned by PHP's exif_read_data() function. It's actually buried inside the "MakerNote" field which exif_read_data() returns as a big block of binary data. As well as exif_read_data, I'm also using the PHP Exif Library (PEL) to write Exif info back into the files. It's pretty powerful, but Exif is a bit of a crazy format, so finding the exact bits you need to extract the data proved problematic. In the end, I could get hold of the binary data for the MakerNote field, but couldn't find a way to parse this as another TIFF container to get at the underlying information.

There's some good information about the layout of this data here, and I did contemplate trying to parse it myself. I found another PHP library (PHP JPEG Metadata Toolkit) which claims to be able to parse Sony MakerNote information, but I didn't have much luck with getting it to run.

In the end, I stumbled across a Perl library for reading Exif data (Exif Tool), this handles the Sony MakerNote field flawlessly and comes with a sample script which can parse the Exif for a photo and return it in a variety of formats.

In the end, I just wrote a wrapper round this script which executes the script from within PHP, getting the results in JSON format and then converting that to a PHP object and returning it. Seems to work flawlessly and the information returned is even better than Flickr, giving the complete name of the lens rather than just "Tamron Lens (255)".

If I get time, I plan to try and work out how Exif Tool decodes the MakerNote and see if I can replicate this in PHP (probably using PEL) but in the meantime, the hack seems to work and now my photos have the lens automatically tagged on upload.

Hopefully, someone trying to do something similar finds this information useful, at least in terms of where to start looking.

Labels: , , , , , , ,

Sunday, October 11, 2009

New Lens - Update 1


Big daisy like flower, originally uploaded by chored.

Yesterday we went for a bit of an exploration of Midlothian. First stop was the newly opened Springfield Mill wildlife park just outside of Loanhead. While we didn't see much "big" wildlife, I got some close-ups of flowers and insects.

The Tamron 17-50 seemed to handle everything pretty well. The maximum magnification is similar to the 70-200 but you can get a lot closer to the subject (27cm minimum focusing distance) which makes taking close-ups really easy.

After Springfield Mill we headed down the road to Roslin Glen to take some more shots. Pretty happy with the lens so far. Seems good even wide open. At 17mm, close-ups aren't that sharp, but I'm thinking, that maybe I should be shooting close-ups that wide :)

Still need to test it out on bigger subjects though.

Labels: , , , , , , ,