- 5 minutes read

You've had some great time off. Maybe you've been traveling a foreign country. So you've been taking pictures diligently. Of course, you don't have to care much about how to organize these pics. Mind you, it's 2018. Your smartphone cares about it all. It orders your photo collection by date, by location, and there's even a map showing where you've taken all the pictures.

At home, you import the entire collection to your PC and upload it to Dropbox. All the extra information gets lost.

But fear not: you're a JavaScript programmer. So it's a piece of cake to access all the metadata hidden in the EXIF section. If you're a Java programmer, that's probably a piece of cake, too. But using node.js you can do in with a just a handful of lines of code. Enjoy!

Looking for a library

Maybe you already know my new JavaScript mantra: there's a library for that.

Not quite. Welcome to the world of JavaScript. There's not only one library. There's a plethora of libraries, each looking promising. For example, piexifjs caught my eye. I can't tell you anything about this library, but the documentation looks great. But it's not the only one. Within a few minutes, I found several libraries. The first library - chosen at random - worked fine. That's exif-parser.

Oh, and don't ask me why I've found piexifjs in my list of bookmarks. It just looked interesting enough to mention it.

Getting your feet wet

Let's start our project with npm init and npm install exif-parser --save. Next, let's create an index.js file:

const exif = require('exif-parser'); const fs = require('fs'); const file = "/folder/containing/a/picture.jpg"; const buffer = fs.readFileSync(file); const parser = exif.create(buffer); const result = parser.parse(); const hdr = result.tags.CustomRendered === 3; const focalLength = result.tags.FocalLengthIn35mmFormat + 'mm';

That's it in a nutshell! Examining the object result exhibits a lot of information about your photo: where you've taken it, which direction you were looking at, exposure time, focal length, and a lot more. In the case of my iPhone 7, even the height above the sea level is recorded. Plus, of course, whether it's an HDR picture or not.

The HDR detection is a peculiarity of Apple's iPhone. By default, the iPhone stores both the original (usually over-exposed) picture and the HDR picture. Regular pictures are stored with CustomRendered: 4 and HDR pics are stored with CustomRendered: 3.

Renaming the image

I assume you're already familiar enough with node.js to rename the file. Nonetheless, my project was about adding the EXIF info to the file name, so let's add the last few lines of code for the sake of completeness:

let newFile = file.substring(0, file.length - 4); newFile += ' ' + focalLength; if (hdr) { newFile = newFile + ' hdr'; } newFile = newFile + '.jpg'; fs.renameSync(file, newFile);

Where to go next

Before long, the GPS coordinates caught my eye. There must be a way to get the name of the village shown in the image! Sure there is. It's only a few lines. Have a look at my article about OpenStreetMap to learn how to do it. The free version of OpenStreetMap, in turn, has a restriction: you are only allowed to use it once per second. So I wrote another article, telling you how to slow down a node.js application deliberately. That's a strange challenge, by the way. Most of the time we're occupied tuning our programs to be as fast as possible. But when you need your program to be slow, we're ill prepared to achieve this goal, at least in node.js.

A side remark about privacy

David Walsh has written an article about Exif data. He'ss a guy who usually has something to say, so I always read his articles when they appear in my list of search results. To my surprise, David's article about Exif metadata isn't about technology, but about privacy. David asks you to be careful when working with images of your customers, your relatives, or your loved ones. In particular, don't publish them on the internet without thinking twice. The GPS coordinates and the timestamp of the image might reveal sensitive data - such as regular habits of the people on the picture. For instance, if they have time off in France every year in August, robbers may "visit" their house next August. So please delete every Exif metadata before publishing the image on the internet (or somewhere else).

Wrapping it up

It's such a blessing to be a programmer. Mind you: reading the Exif metadata and making use of it is a fairly common problem, there's almost certainly a standard software product doing the job. But how much time does it take to find it? How much do you pay for licenses and fees? Not to mention the disappointments you're likely to experience when the people behind the tool of your choice find out how to make more money off their product. That's what happened to my long-time favorite: after an update the "rename file" function I always used was gone.

All that's unlikely to happen with your own, tiny node.js script!


Comments