Insert exif direclty into image file.

This commit is contained in:
Mr Finchum 2025-01-12 16:57:28 +01:00
parent bc735b6830
commit 646083daff
2 changed files with 27 additions and 5 deletions

View file

@ -13,8 +13,8 @@ class OptimaManager:
def modify_timestamp_in_exif(self, data_for_exif: dict, filename: str):
""""Takes a dict formated for exif use by piexif and adjusts the date_time_original, changing the minutes and seconds to fit the number of the filname."""
last_tree = filename[-3:len(filename)]
total_seconds = int(re.sub(r'\D+', '', last_tree))
last_three = filename[-3:len(filename)]
total_seconds = int(re.sub(r'\D+', '', last_three))
minutes = total_seconds // 60
seconds = total_seconds % 60
time = datetime.strptime(data_for_exif["date_time_original"], "%Y:%m:%d %H:%M:%S") # change date time string back to an time object for modification
@ -44,7 +44,6 @@ class OptimaManager:
with self.image_processor.open_image(image_input_file) as img:
processed_img = img
image_name = os.path.basename(image_output_file) # for date adjustment
# Resize
if resize is not None:
processed_img = self.image_processor.resize_image(
@ -79,7 +78,7 @@ class OptimaManager:
selected_exif = dict_for_exif
if "date_time_original" in dict_for_exif:
selected_exif = self.modify_timestamp_in_exif(selected_exif, image_name)
exif_piexif_format = self.exif_handler.build_exif_dict(
exif_piexif_format = self.exif_handler.build_exif_bytes(
selected_exif, self.image_processor.get_image_size(processed_img)
)
@ -111,3 +110,22 @@ class OptimaManager:
)
else:
return self.image_processor.convert_pil_to_qtimage(processed_img)
def insert_dict_to_image(self, exif_dict, image_path, gps = None):
image_name, ending = os.path.splitext(os.path.basename(image_path))
img = self.image_processor.open_image(image_path)
selected_exif = exif_dict
if "date_time_original" in exif_dict:
selected_exif = self.modify_timestamp_in_exif(selected_exif, image_name)
exif_piexif_format = self.exif_handler.build_exif_bytes(
selected_exif, self.image_processor.get_image_size(img)
)
# GPS data
if gps is not None:
latitude = float(gps[0])
longitude = float(gps[1])
exif_piexif_format = self.exif_handler.add_geolocation_to_exif(exif_piexif_format, latitude, longitude)
self.exif_handler.insert_exif(exif_dict = exif_piexif_format, img_path = image_path)

View file

@ -103,7 +103,7 @@ class ExifHandler:
def get_exif_info(self, image):
return(piexif.load(image.info['exif']))
def build_exif_dict(self, user_data, imagesize):
def build_exif_bytes(self, user_data, imagesize):
"""Build a piexif-compatible EXIF dictionary from a dicts."""
# Mostly made by ChatGPT, some adjustment
zeroth_ifd = {
@ -201,3 +201,7 @@ class ExifHandler:
return exif_data
except Exception as e:
print(f"Error: {str(e)}")
def insert_exif(self, exif_dict, img_path):
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, img_path)