Adjust image_processing and its friends to wrk with EXIF formatted date

This commit is contained in:
Fred Boniface 2023-11-06 20:28:51 +00:00
parent 11517a7bea
commit 4f33221208
3 changed files with 21 additions and 4 deletions

View File

@ -1,10 +1,17 @@
import pyexiv2 import pyexiv2
from datetime import datetime
def get_image_data(path: str): def get_image_data(path: str):
print(f"Reading metadata: {path}") print(f"Reading metadata: {path}")
img = pyexiv2.Image(path) img = pyexiv2.Image(path)
xmp = img.read_xmp() xmp = img.read_xmp()
create_date = xmp.get('Xmp.xmp.CreateDate', 'N/A') 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"
title = xmp.get('Xmp.dc.title', 'N/A') title = xmp.get('Xmp.dc.title', 'N/A')
description = xmp.get('Xmp.dc.description', 'N/A') description = xmp.get('Xmp.dc.description', 'N/A')
tags = xmp.get('Xmp.dc.subject', 'N/A') tags = xmp.get('Xmp.dc.subject', 'N/A')
@ -13,7 +20,7 @@ def get_image_data(path: str):
alt_text = description_parts[1].strip() alt_text = description_parts[1].strip()
image_data = { image_data = {
'path': path, 'path': path,
'create': create_date + "Z", 'create': create_date,
'title': title['lang="x-default"'], 'title': title['lang="x-default"'],
'description': main_description, 'description': main_description,
'alt': alt_text, 'alt': alt_text,
@ -24,4 +31,14 @@ def get_image_data(path: str):
def add_watermark(file_data): def add_watermark(file_data):
## Create a new image file in the same directory as the file_data['path'] and return ## 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'] ## a new file_data dictionary with the new image in place of file_data['path']
return file_data 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

View File

@ -1,5 +1,4 @@
import os, toml import os, toml
from dotenv import load_dotenv
config = toml.load("conf.toml") config = toml.load("conf.toml")

View File

@ -26,6 +26,7 @@ def formatTags(tags: list):
return formatted_tags_str return formatted_tags_str
def formatDate(input_date_str: str): def formatDate(input_date_str: str):
print(input_date_str)
input_datetime = datetime.datetime.fromisoformat(input_date_str) input_datetime = datetime.datetime.fromisoformat(input_date_str)
london_tz = pytz.timezone('Europe/London') london_tz = pytz.timezone('Europe/London')
london_datetime = input_datetime.replace(tzinfo=pytz.utc).astimezone(london_tz) london_datetime = input_datetime.replace(tzinfo=pytz.utc).astimezone(london_tz)