// Run this script in node.js // // First install deps: // sudo apt-get install libexiv2-12 libexiv2-dev // npm install exiv2 // // Argument #1 = .JPG relative path // Argument #2 = overwrite file true/false. False will create a new file with ".jpg.jpg" extension // "raspicam-fix-exif.js" ar ex = require('exiv2'); var fs = require('fs'); // overwrite or not? true/false var _OVERWRITE=process.argv[3]; // the file name to read exif //example '/tmp/RPIPICI13120400001.jpg'; var fName = process.argv[2]; // make a backup which should be overwriten // with the fixed tags // function copyFile(origFName, newFName, cb) { var fCalledBack = false; var readIn = fs.createReadStream(origFName); readIn.on("error", function(err) { callback(err); }); var writeOut = fs.createWriteStream(newFName); writeOut.on("error", function(err) { callback(err); }); writeOut.on("close", function(ex) { callback(); }); readIn.pipe(wr); function callback(err) { if (!fCalledBack) { // call the callback function which was passed in. cb(origFName, err); // to be safe, in case err/close trigger arrives more than once fCalledBack = true; } } }; // Get the exif info // function getImageTags(fName, cb) { console.log ("reading exif... " + fName); ex.getImageTags(fName, function(err, tags) { if (err) throw err; var dateTime = tags["Exif.Image.DateTime"]; var dateTimeOriginal = tags["Exif.Photo.DateTimeOriginal"]; var dateTimeDigitized = tags["Exif.Photo.DateTimeDigitized"]; // print the old values console.log("\tFilename: " + fName + " :: \n" + "\tDateTime: " + dateTime + "\n" + "\tDateTimeOriginal: " + dateTimeOriginal + "\n" + "\tDateTimeDigitized: " + dateTimeDigitized + "\n"); // fix the old values. Replace the ":" in between date and time with a " ". dateTime = dateTime.substr(0, 10) + " " + dateTime.substr(11, 20); dateTimeOriginal = dateTimeOriginal.substr(0, 10) + " " + dateTimeOriginal.substr(11, 20); dateTimeDigitized = dateTimeDigitized.substr(0, 10) + " " + dateTimeDigitized.substr(11, 20); // put then in a new tag array var newTags = { "Exif.Image.DateTime" : dateTime, "Exif.Photo.DateTimeOriginal" : dateTimeOriginal, "Exif.Photo.DateTimeDigitized" : dateTimeDigitized }; // callback the caller cb (fName + (_OVERWRITE ? "" : ".jpg"), newTags); }); }; // Write out the new exif tags to the new jpg file // function setImageTags (newFName, newTags) { // now save the exif info ex.setImageTags(newFName, newTags, function(err) { console.log ("Exif set = " + (err ? ("Error: " + err) : "Success")); }); // set image }; // Called either directly or after the copy file (when not overwriting) is complete function retag(fName, err) { if (err) throw err; console.log ("getting...."); // lets get the original tags getImageTags (fName, function (newFName, newTags) { console.log("Will soon set tags to " + newFName); setImageTags (newFName, newTags); }); }; console.log ("working....Overwrite=" + _OVERWRITE + " Fname=" + fName); // This is the primary control. Either overwrite or not. if (_OVERWRITE) { console.log ("overwriting\n"); // get the tags. Overwrite the original file with the fixed date flags. retag(fName); } else { // Do not overwrite the file. Instead name it "*.jgp.jpg" console.log ("not overwrite\n"); // Start by copying the file where the new exif info will go // Pass the "retag" function as the callback, so once the copy // is complete, the flow will continue as normal // copyFile (fName, fName + ".jpg", retag); }