如何将元数据添加到 Google Photos 中的图片

How can I add meta data to the picture in Google Photos

我刚刚通过 api 创建了我的第一张相册,上传了一张照片并添加到我的新相册中。但是如何指定时区、相机数据、位置等?

我真的需要通过 exif 嵌入这些信息吗?在那种情况下,我需要操纵我想避免的图像数据。

这是我的解决方法。我通过 exif 在 nodejs 中添加信息:

request.get('http://example.com/foto.jpg')
    .pipe(exifEnricher)
    .pipe(request.post('https://photoslibrary.googleapis.com/v1/uploads', {
        headers: {
            'User-Agent': 'MyService',
            'Authorization': 'Bearer ' + token,
            'Content-type': 'application/octet-stream',
            'X-Goog-Upload-File-Name': 'Filename.jpg',
            'X-Goog-Upload-Protocol': 'raw'
        }
    }, function(error, uploadResponse, uploadToken) {
        if(error) {
            response.end(error);
        } else {
            request.post('https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate', {
                headers: {
                    'User-Agent': 'MyService',
                    'Authorization': 'Bearer ' + token,
                    'Content-type': 'application/json'
                },
                json: {
                    "albumId": "abcdef...",
                    "newMediaItems": [
                        {
                            "description": "Haustür X",
                            "simpleMediaItem": {
                                "uploadToken": uploadToken
                            }
                        }
                    ],
                    "albumPosition": {
                        "position": "FIRST_IN_ALBUM"
                    }
                }
            }, (error, addToAlbumResponse, addToAlbumBody) => {
                if(error) {
                    response.end(error);
                } else {
                    response.end(JSON.stringify(addToAlbumBody));
                }
            });
        }
    }));

const exifEnricher = new Transform({
    transform(chunk, encoding, callback) {
        if(chunk[0] === 255 && chunk[1] === 216) {
            const data = chunk.toString("binary");
            const exifObj = piexif.load(data);
            exifObj["GPS"][piexif.GPSIFD.GPSVersionID] = [2, 2, 0, 0];
            exifObj["GPS"][piexif.GPSIFD.GPSLatitudeRef] = "N";
            exifObj["GPS"][piexif.GPSIFD.GPSLatitude] = [[lat_degree, 1], [lat_minutes, 1], [lat_seconds_x_10, 10]];
            exifObj["GPS"][piexif.GPSIFD.GPSLongitudeRef] = "E";
            exifObj["GPS"][piexif.GPSIFD.GPSLongitude] = [[lon_degree, 1], [lon_minutes, 1], [lon_seconds_x_10, 10]];
            exifObj["GPS"][piexif.GPSIFD.GPSAltitudeRef] = 0;
            exifObj["GPS"][piexif.GPSIFD.GPSAltitude] = [alt_in_cm, 100];
            exifObj["0th"] = exifObj["0th"] || {};
            exifObj["0th"][piexif.ImageIFD.Software] = "MyService";
            exifObj["0th"][piexif.ImageIFD.Make] = "Brand";
            exifObj["0th"][piexif.ImageIFD.Model] = "Device Name";
            exifObj["0th"][piexif.ImageIFD.DateTime] = dateFormat(new Date(), "yyyy:mm:dd HH:mm:ss");
            const exifbytes = piexif.dump(exifObj);
            const newData = piexif.insert(exifbytes, data);
            callback(null, Buffer.from(newData, 'binary'))
        } else {
            callback(null, chunk)
        }
    }
});