social-photos/social_photos/image_processing.py

44 lines
1.5 KiB
Python
Raw Normal View History

2023-11-04 22:29:00 +00:00
import pyexiv2
from datetime import datetime
2023-11-04 22:29:00 +00:00
def get_image_data(path: str):
print(f"Reading metadata: {path}")
2023-11-04 22:29:00 +00:00
img = pyexiv2.Image(path)
xmp = img.read_xmp()
print(xmp)
create_date = xmp.get('Xmp.xmp.CreateDate', None)
if create_date is None:
create_date = convert_datetime_to_iso(xmp.get('Xmp.exif.DateTimeOriginal', 'N/A'))
else:
create_date = create_date + "Z"
2023-11-04 22:29:00 +00:00
title = xmp.get('Xmp.dc.title', 'N/A')
description = xmp.get('Xmp.dc.description', 'N/A')
tags = xmp.get('Xmp.dc.subject', 'N/A')
description_parts = description['lang="x-default"'].split('|')
main_description = description_parts[0].strip()
alt_text = description_parts[1].strip()
image_data = {
'path': path,
'create': create_date,
2023-11-04 22:29:00 +00:00
'title': title['lang="x-default"'],
'description': main_description,
'alt': alt_text,
'tags': tags
}
2023-11-05 19:26:47 +00:00
return image_data
def add_watermark(file_data):
## Create a new image file in the same directory as the file_data['path'] and return
## a new file_data dictionary with the new image in place of file_data['path']
return file_data
def convert_datetime_to_iso(datetime_str):
try:
# Parse the datetime string using the format 'YYYY:MM:DD HH:MM:SS.sss'
datetime_obj = datetime.strptime(datetime_str, '%Y:%m:%d %H:%M:%S.%f')
# Convert the datetime to ISO format
iso_datetime = datetime_obj.isoformat()
return iso_datetime
except ValueError:
return None