Skip to content

Instantly share code, notes, and snippets.

@KevinGutowski
Created September 13, 2021 03:37
Show Gist options
  • Select an option

  • Save KevinGutowski/7e36655a88f4f3b801d44f759e429cb2 to your computer and use it in GitHub Desktop.

Select an option

Save KevinGutowski/7e36655a88f4f3b801d44f759e429cb2 to your computer and use it in GitHub Desktop.
Update Facebook Photos Export with EXIF data
import os,json,pyexiv2
from datetime import datetime
keypath = 'fbexport/'
path_to_album= keypath+'posts/album/'
json_files = [pos_json for pos_json in os.listdir(path_to_album) if pos_json.endswith('.json')]
for album_json in json_files:
with open(path_to_album+album_json) as f:
data = json.load(f)
for photo_data in data['photos']:
uri = keypath + photo_data['uri']
creation_timestamp = photo_data['creation_timestamp']
ct = datetime.utcfromtimestamp(creation_timestamp)
title = photo_data['title']
description = photo_data['description'] if 'description' in photo_data else ""
caption = title+", "+description
img = pyexiv2.Image(uri)
xmpDict = {
'Xmp.photoshop.DateCreated': ct.strftime("%Y-%m-%dT%H:%M:%S"),
'Xmp.xmp.ModifyDate': ct.strftime("%Y-%m-%dT%H:%M:%S"),
'Xmp.xmp.CreateDate': ct.strftime("%Y-%m-%dT%H:%M:%S"),
'Xmp.xmp.CreatorTool': '14.7.1',
'Xmp.exif.CompositeImage': '2',
'Xmp.dc.description': {'lang="x-default"': caption}
}
iptcDict = {
'Iptc.Envelope.CharacterSet': '\x1b%G',
'Iptc.Application2.RecordVersion': '2',
'Iptc.Application2.DigitizationTime': ct.strftime("%H:%M:%S+00:00"),
'Iptc.Application2.DigitizationDate': ct.strftime("%Y-%m-%d"),
'Iptc.Application2.TimeCreated': ct.strftime("%H:%M:%S+00:00"),
'Iptc.Application2.DateCreated': ct.strftime("%Y-%m-%d"),
'Iptc.Application2.Caption': caption
}
exifDict = {
'Exif.Image.ImageDescription': caption,
'Exif.Image.DateTime': ct.strftime("%Y:%m:%d %H:%M:%S"),
'Exif.Image.ExifTag': '278',
'Exif.Photo.ExifVersion': '48 50 51 50',
'Exif.Photo.DateTimeOriginal': ct.strftime("%Y:%m:%d %H:%M:%S"),
'Exif.Photo.DateTimeDigitized': ct.strftime("%Y:%m:%d %H:%M:%S")
}
img.modify_xmp(xmpDict)
img.modify_iptc(iptcDict)
img.modify_exif(exifDict)
img.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment