добавить / изменить координаты GPS на jpeg Exif с помощью piexifjs

я использую piexifjs to manipulate exif data of jpg images. everything works fine. but when I try to modify the gps longitude and latitudes, I am having some issues.
as on https://www.exiv2.org/tags.html, it states the datatype for gps coordinates it a Rational and I am having trouble getting how it works.

      gps[piexif.GPSIFD.GPSLatitude] =  23.234;
gps[piexif.GPSIFD.GPSLatitudeRef] = "S";

OR

      gps[piexif.GPSIFD.GPSLatitude] = [23,23,23];
gps[piexif.GPSIFD.GPSLatitudeRef] = "S";

I can add all the others like Author, XPTitle... and yet it It doesn't work at all for gps coordinates.

1 ответ

This worked for me.
It accepts in an array of dimention [3,2] eg. [[59, 1], [26, 1], [794, 100]]

      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]]
  }

thanks to https://github.com/hMatoba/piexifjs/issues/1#issuecomment-260176317

Другие вопросы по тегам