EmbedMetadata/standards/exif.py

81 lines
2.2 KiB
Python
Raw Normal View History

import logging
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
allowed_exts=[
"tif","tiff", # TIFF
"jpeg","jpg","pjpeg","pjpg","jpe", # JPEG
"webp", # Webp
# "wav","wave", # RIFF Wave
"png","apng" # PNG (since 1.2)
]
def backupMetadata(file):
from PIL import Image, ExifTags
logging.debug(f"loading file {file}")
img = Image.open(file)
logging.debug("file loaded. getting exif")
img_exif = img.getexif()
full=[]
if img_exif is None:
logging.warning("Could not load EXIF Data... Either something happened with Pillow, the file disappeared, or you don't have access to the file. Try running verbose!")
break
else:
logging.debug("loaded tags")
for key, val in img_exif.items():
logging.debug(f"for key:'{key}', val:'{val}' in img_exif.items()")
if key in ExifTags.TAGS:
logging.debug(f"{key} is known exif tag! ({ExifTags.TAGS[key]})")
full.append({
'tagName': str(key),
'realName': str(ExifTags.TAGS[key]),
'value': str(val)
})
else:
logging.warning(f"{key} is an unknown exif tag!")
full.append({
'tagName': str(key),
'realName': None,
'value': str(val)
})
logging.info("writing backup data")
import json
#import tempfile
#temp = tempfile.TemporaryFile(mode='w+')
#json.dump(full,temp,sort_keys=True)
with open(f"{file}.exif_backup.json.xz",'w+b') as bfile
logging.debug(f"writing to {bfile}")
import lzma
bfile.write(lzma.compress(
bytes(json.dumps(full,sort_keys=True)),
format=FORMAT_XZ,
check=CHECK_SHA256,
preset=9
))
logging.debug("wrote backup data")
logging.info("backup done!")
def writeTags(file,tags):
from PIL import Image
image=Image.open(file)
exif = image.getexif()
if artist in tags:
if type(tag.artist) == str:
exif[0x013b] = tags.artist
elif tags.category == "deviantart":
exif[0x013b] = tags.artist.username
if title in tags:
exif[0x0010e] = tags.title
if description in tags:
exif[0x9286] = tags.description