2024-12-30 23:36:18 +01:00
import re
2024-12-31 12:05:28 +01:00
import os
2024-12-30 23:36:18 +01:00
from datetime import datetime
2025-01-02 15:10:53 +01:00
from optima . image_handler import ImageProcessor , ExifHandler
2024-12-30 23:36:18 +01:00
class OPTIMA35 :
def __init__ ( self ) :
self . name = " OPTIMA-35 "
2025-01-02 15:10:53 +01:00
self . version = " 0.5.0 "
2024-12-30 23:36:18 +01:00
self . image_processor = ImageProcessor ( )
self . exif_handler = ExifHandler ( )
2025-01-02 15:10:53 +01:00
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 ) )
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
new_time = time . replace ( hour = 12 , minute = minutes , second = seconds )
data_for_exif [ " date_time_original " ] = new_time . strftime ( " % Y: % m: %d % H: % M: % S " )
return data_for_exif
2024-12-30 23:36:18 +01:00
2025-01-02 15:10:53 +01:00
def process_image ( self ,
image_input_file ,
image_output_file ,
file_type ,
quality ,
compressing ,
optimize ,
resize = None ,
watermark = None ,
font_size = 2 ,
grayscale = False ,
brightness = None ,
contrast = None ,
dict_for_exif = None ,
gps = None ,
copy_exif = False ) :
# Partly optimized by ChatGPT
# Open the image file
2024-12-30 23:36:18 +01:00
with self . image_processor . open_image ( image_input_file ) as img :
processed_img = img
2025-01-02 15:10:53 +01:00
image_name = os . path . basename ( image_output_file ) # for date adjustment
2024-12-30 23:36:18 +01:00
2025-01-02 15:10:53 +01:00
# Resize
if resize is not None :
2024-12-30 23:36:18 +01:00
processed_img = self . image_processor . resize_image (
2025-01-02 15:10:53 +01:00
image = processed_img , percent = resize
)
# Watermark
if watermark is not None :
processed_img = self . image_processor . add_watermark (
processed_img , watermark , int ( font_size )
2024-12-30 23:36:18 +01:00
)
2025-01-02 15:10:53 +01:00
# Grayscale
if grayscale :
2024-12-30 23:36:18 +01:00
processed_img = self . image_processor . grayscale ( processed_img )
2025-01-02 15:10:53 +01:00
# Brightness
if brightness is not None :
processed_img = self . image_processor . change_brightness (
processed_img , brightness
)
# Contrast
if contrast is not None :
processed_img = self . image_processor . change_contrast (
processed_img , contrast
)
# EXIF data handling
exif_piexif_format = None
if dict_for_exif : # todo: maybe move to ui and only accept complete exif dicts..
selected_exif = dict_for_exif
if " date_time_original " in dict_for_exif :
2024-12-30 23:36:18 +01:00
selected_exif = self . modify_timestamp_in_exif ( selected_exif , image_name )
2025-01-02 15:10:53 +01:00
exif_piexif_format = self . exif_handler . build_exif_dict (
selected_exif , self . image_processor . get_image_size ( processed_img )
)
2024-12-30 23:36:18 +01:00
2025-01-02 15:10:53 +01:00
# 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 )
# Copy EXIF data if selected, and ensure size is correct in exif data
elif copy_exif :
2024-12-30 23:36:18 +01:00
try :
og_exif = self . exif_handler . get_exif_info ( img )
og_exif [ " Exif " ] [ 40962 ] , og_exif [ " Exif " ] [ 40963 ] = self . image_processor . get_image_size ( processed_img )
2025-01-02 15:10:53 +01:00
exif_piexif_format = og_exif
2024-12-30 23:36:18 +01:00
except Exception :
print ( " Copying EXIF data selected, but no EXIF data is available in the original image file. " )
2025-01-02 15:10:53 +01:00
# Save the processed image
2024-12-30 23:36:18 +01:00
self . image_processor . save_image (
image = processed_img ,
path = image_output_file ,
2025-01-02 15:10:53 +01:00
piexif_exif_data = exif_piexif_format ,
file_type = file_type ,
jpg_quality = quality ,
png_compressing = compressing ,
optimize = optimize
2024-12-30 23:36:18 +01:00
)