I’m working on using my Flickr export. I naively thought that working with EXIF metadata from images in Python would be easy in 2019. I was wrong. Particularly if you want to write EXIF data. I’m tentatively trying PyExifTool, a wrapper for a Perl command line program. More on that below.
PyPI has 126 packages for EXIF. But a closer look shows a lot of them are bad in various ways and there’s no one consensus package. Lots of version 0.1s, stuff that was abandoned 4 years ago, etc. PIL/Pillow has EXIF support but it’s very low level and a clunky API. Piexif looks somewhat promising as a pure Python thing but also very clunky API.
ExifTool is pretty much the Swiss army chainsaw for doing these kinds of things. It has a steep learning curve, but once you're over it, the kind of renaming you're after is a snap: exiftool -d '%Y%m%d-%H%M%%-03.c.%%e' '-filenameExifTool to format dates according to the next argument's pattern. Jan 22, 2019 PyExifTool. PyExifTool is a Python library to communicate with an instance of Phil Harvey's excellent ExifTool command-line application. The library provides the class exiftool.ExifTool that runs the command-line tool in batch mode and features methods to send commands to that program, including methods to extract meta-information from one or more image files.
Turns out the real problem is EXIF is a mess. When we say EXIF we actually mean three separate metadata formats: EXIF itself, the older IPTC tags, and the more modern inlineed XMP tags. Some of these formats have overlapping metadata in them. EXIF itself is a real mess of a binary format. For instance the GPS location info looks more like raw data from a GPS chip than a simple WGS location. For instance it supports multiple formats for lat/lon. Want decimals? minutes? seconds? Some mix? You can write anything!
The gold standard for good EXIF+others support is ExifTool, a 15 year old Perl library / command line program. It has an active community of developers who’ve spent a long time fiddling with all the weirdness in image metadata formats and making a bulletproof tool that can read and write them. So why try to rewrite all that code in Python?
Enter smarnach’s pyexiftool and the several separately packaged versions of it on PyPI. It’s four years old and as far as I can tell there is no active fork. Fortunately the code is pretty simple.
![Python Python](/uploads/1/1/9/4/119423710/535679161.jpg)
Write Exif Python
The main thing it does is fork a single exiftool process with the -stay_open flag. That causes the Perl program to keep reading commands. The rest of the Python code is a little wrapper around the JSON api and some convenience code if all you want is to read metadata. It seems to work.
Pyexiftool
So there we go; I’m gonna be writing EXIF tags by passing command line arguments over a file descriptor to a running Perl program. It’s a kludge, but if it works I’ll take it.