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.
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: coding, exif, flickr, makernote, perl, photography, php, sony
2 Comments:
Very funny!! You wrote: "[ExifTool is] even better than Flickr".
In fact, Flickr actually uses ExifTool for its metadata output, but the problem is they haven't bothered to update their ExifTool version since they added this feature over a year ago!
- Phil
Haha, that's interesting to know.
I realise now that there isn't actually any information in the MakerNote for Tamron lenses other than the "Tamron (255)" code and that ExifTool must use the aperture/focal length to work out which of the Tamron lenses could have taken the photo.
Post a Comment
<< Home