Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
923 views
in Technique[技术] by (71.8m points)

google apps script - How to extract EXIF data from jpeg files in Drive

I am trying to extract EXIF data from several jpeg files located in one of my Google Drive folders, using a Google Script.

More accurately, I would like to extract the date at which the photo was taken, together with associated keywords / description of the image, previously created with Adobe Lightroom.

I know that several scripts allowing to extract EXIF data from a file exist on internet, but I didn't manage to link them, or to use them with my own Google script.

How can I do that easily ? (I am a beginner on Google Script, please be as precise as possible)

Thank you in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The DriveApp service of Google Apps Script doesn't provide access to the details you're looking for, but the Advance Drive Service does.

Code.gs

You will need to enable the Advanced Drive service, following instructions here.

// Demo use of getPhotoExif()
// Logs all files in a folder named "Photos".
function listPhotos() {
  var files = DriveApp.getFoldersByName("Photos").next().getFiles();
  var fileInfo = [];
  while (files.hasNext()) {
    var file = files.next();
    Logger.log("File: %s, Date taken: %s",
               file.getName(),
               getPhotoExif(file.getId()).date || 'unknown');
  }
}

/**
 * Retrieve imageMediaMetadata for given file. See Files resource
 * representation for details.
 * (https://developers.google.com/drive/v2/reference/files)
 *
 * @param {String} fileId    File ID to look up
 *
 * @returns {object}         imageMediaMetadata object
 */
function getPhotoExif( fileId ) {
  var file = Drive.Files.get(fileId);
  var metaData = file.imageMediaMetadata;

  // If metaData is 'undefined', return an empty object
  return metaData ? metaData : {};
}

In the Google Drive API, the resource representation for a File includes properties for the EXIF data:

  "imageMediaMetadata": {
    "width": integer,
    "height": integer,
    "rotation": integer,
    "location": {
      "latitude": double,
      "longitude": double,
      "altitude": double
    },
    "date": string,
    "cameraMake": string,
    "cameraModel": string,
    "exposureTime": float,
    "aperture": float,
    "flashUsed": boolean,
    "focalLength": float,
    "isoSpeed": integer,
    "meteringMode": string,
    "sensor": string,
    "exposureMode": string,
    "colorSpace": string,
    "whiteBalance": string,
    "exposureBias": float,
    "maxApertureValue": float,
    "subjectDistance": integer,
    "lens": string
  },

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...