Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing GPS exif data into image in Node

Good evening, community.

I have a question regarding changing exif meta data on jpegs using node.js. I have a set of coordinates which I need to attach to the image file, but for some reason, I cannot find the right library on npm for that. There are plenty of extracting libraries, like exif, exif-js, no-exif and so on, but all of the are retrieving data from images. I'm going the opposite direction, and extracting coordinates/gps data from the kml file and based on that updating the images, which do not have geo-location metadata.

What is the best approach for doing this?

like image 774
Midnight Coder Avatar asked Apr 17 '15 00:04

Midnight Coder


2 Answers

I have written a library to modify exif on client-side. It would help you even on Node.js. https://github.com/hMatoba/piexifjs

I tried to run the library on Node.js. No error occurred and got a new jpeg modified exif.

var piexif = require("piexif.js");
var fs = required("fs");

var jpeg = fs.readFileSync(filename1);
var data = jpeg.toString("binary");
var exifObj = piexif.load(data);
exifObj["GPS"][piexif.GPSIFD.GPSVersionID] = [7, 7, 7, 7];
exifObj["GPS"][piexif.GPSIFD.GPSDateStamp] = "1999:99:99 99:99:99";
var exifbytes = piexif.dump(exifObj);
var newData = piexif.insert(exifbytes, data);
var newJpeg = new Buffer(newData, "binary");
fs.writeFileSync(filename2, newJpeg);
like image 89
hMatoba Avatar answered Oct 15 '22 02:10

hMatoba


This worked best for me. eg. to write a coordinate of 23.2342 N, 2.343 W

const piexifjs = require("piexifjs");

var filename1 = "image.jpg";
var filename2 = "out.jpg";

var jpeg = fs.readFileSync(filename1);
var data = jpeg.toString("binary");

var exifObj = piexif.load(data);

exifObj.GPS[piexif.GPSIFD.GPSLatitude] =  degToDmsRational(23.2342);
exifObj.GPS[piexif.GPSIFD.GPSLatitudeRef] =  "N";
exifObj.GPS[piexif.GPSIFD.GPSLongitude] =  degToDmsRational(2.343);
exifObj.GPS[piexif.GPSIFD.GPSLongitudeRef] =  "W";

var exifbytes = piexif.dump(exifObj);

var newData = piexif.insert(exifbytes, data);
var newJpeg = Buffer.from(newData, "binary");
fs.writeFileSync(filename2, newJpeg);



function degToDmsRational(degFloat) {
    var minFloat = degFloat % 1 * 60
    var secFloat = minFloat % 1 * 60
    var deg = Math.floor(degFloat)
    var min = Math.floor(minFloat)
    var sec = Math.round(secFloat * 100)

    deg = Math.abs(deg) * 1
    min = Math.abs(min) * 1
    sec = Math.abs(sec) * 1
  
    return [[deg, 1], [min, 1], [sec, 100]]
  }
like image 27
Abraham Avatar answered Oct 15 '22 01:10

Abraham