Skip to content

Instantly share code, notes, and snippets.

@pirosuke
Last active Jan 21, 2019
Embed
What would you like to do?
A Sample Script To Get EXIF Data From Image File.
const path = require('path');
const fs = require('fs');
const piexif = require('piexifjs');
function getExifFromImage(imgPath) {
const imgData = fs.readFileSync(imgPath).toString('binary');
const exifRawData = piexif.load(imgData);
const exifData = {};
for (const exifType in exifRawData) {
if (exifType === 'thumbnail') {
continue;
}
if (!exifData.hasOwnProperty(exifType)) {
exifData[exifType] = {};
}
for (const tag in exifRawData[exifType]) {
const tagName = piexif.Tags[exifType][tag]['name'];
exifData[exifType][tagName] = exifRawData[exifType][tag];
}
}
return exifData;
}
async function main() {
const srcDirPath = 'SOURCE_DIRECTORY_PATH';
const srcFileList = fs.readdirSync(srcDirPath);
for (const srcFileName of srcFileList) {
if (!srcFileName.endsWith('.JPG')) {
continue;
}
console.log('Processing ', srcFileName, '...');
try {
const exifData = getExifFromImage(path.join(srcDirPath, srcFileName));
for (const exifType in exifData) {
console.log(exifType, '----------');
for (const tagName in exifData[exifType]) {
console.log(tagName + ': ' + exifData[exifType][tagName]);
}
console.log();
}
} catch (err) {
console.log(err);
}
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment