Geo Tagging Photos with Ruby

Recenty I started to use my Sony a5000 more often mostly because my phone camera is pretty bad, and I don’t want to blame myself later. The camera is pretty good, but lack one important feature for me: it does not record geo information into the photos like all of the phones do.

After quick googling, I settled with using my phone as external GPS tracker and record tracks into GPX format. Cheep and efficient when you have external battery :). But the software to inject the tags was open question. As I don’t use any photo organizer except google photos (which is more like a storage rather than organizer), using any desktop solution looked like overkill. More over they usually do not support Linux and/or FreeBSD. So I came to scripting solution.

In ruby land, there are few EXIF libraries, with different features and capabilities. I will just enumerate some of them:

Eventually I set to write trivial ruby wrapper around libexif and it resulted in exif_geo_tag library. It handles only tags defined in “GPS Attribute Information” section of Exif 2.2 specification and does it efficiently. In addition to writing tags directly, it allows to specify “virtual” tags, which handle type conversion (full list of tags in README). For example instead of specifying coordinates in form of degrees, minutes and seconds:

ExifGeoTag.write_tag('/tmp/photo.jpg', longitude: [18, 46, 20.284],
                                       latitude: [42, 25, 32.372])

you can just use fields with underscore with float values and it will perform appropriate conversion (which is useful when working with GPX data):

ExifGeoTag.write_tag('/tmp/photo.jpg', _longitude: 18.772301111111112,
                                       _latitude: 42.42565888888889)

In my script I also use to load Navitel GPX files with small patch I hope eventually get into the upstream. If you are curious, my full script for tagging and sorting out photos before uploading to Google Photos is here: classify.rb.