Refactored code to be more maintainable
This commit is contained in:
@@ -23,7 +23,8 @@ def get_first_image_path():
|
||||
return os.path.join(root, file)
|
||||
return None
|
||||
|
||||
def process_directory_with_logging(additional_name, is_checked, log, update_previews):
|
||||
def process_directory_with_logging(selected_directory: str, additional_name: str = '', is_checked: bool = False, log = None, update_previews = None):
|
||||
print(f"is_checked: {is_checked}")
|
||||
if not selected_directory:
|
||||
messagebox.showwarning("No Directory", "Please select a directory.")
|
||||
return
|
||||
@@ -55,13 +56,10 @@ def process_directory_with_logging(additional_name, is_checked, log, update_prev
|
||||
output_path = os.path.join(output_directory, os.path.relpath(file_path, selected_directory))
|
||||
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
||||
resize_image(file_path, output_path, additional_name)
|
||||
|
||||
# with tempfile.NamedTemporaryFile(suffix='.jpg', delete=False) as temp_file:
|
||||
# temp_output_path = temp_file.name
|
||||
# resize_image(file_path, temp_output_path, additional_name)
|
||||
# update_previews(file_path, temp_output_path)
|
||||
|
||||
|
||||
if os.path.exists(file_path) and is_checked:
|
||||
if log:
|
||||
log(f"removing: {file_path}")
|
||||
os.remove(file_path)
|
||||
if log:
|
||||
log(f"Processed: {file_path}")
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
import os
|
||||
from wand.image import Image
|
||||
from wand.color import Color
|
||||
import os
|
||||
|
||||
|
||||
canvas_width = 900
|
||||
canvas_height = 900
|
||||
|
||||
def set_canvas_size(width, height):
|
||||
global canvas_width, canvas_height
|
||||
@@ -12,6 +8,11 @@ def set_canvas_size(width, height):
|
||||
canvas_height = int(height)
|
||||
|
||||
def resize_image(image_path, output_path, additional_name):
|
||||
|
||||
# Normalize the paths to ensure consistency
|
||||
image_path = os.path.normpath(image_path)
|
||||
output_path = os.path.normpath(output_path)
|
||||
|
||||
with Image(filename=image_path) as img:
|
||||
img.transform(resize=f'{canvas_width}x{canvas_height}>')
|
||||
|
||||
@@ -20,9 +21,14 @@ def resize_image(image_path, output_path, additional_name):
|
||||
|
||||
with Image(width=canvas_width, height=canvas_height, background=Color('transparent')) as canvas:
|
||||
canvas.composite(img, left=x_offset, top=y_offset)
|
||||
# Create a new filename
|
||||
new_filename = os.path.splitext(os.path.basename(output_path))[0]
|
||||
if additional_name:
|
||||
new_filename += " - " + additional_name.strip()
|
||||
new_filename += os.path.splitext(output_path)[1]
|
||||
output_path = os.path.join(os.path.dirname(output_path), new_filename)
|
||||
canvas.save(filename=output_path)
|
||||
# Construct the final output path
|
||||
final_output_path = os.path.join(os.path.dirname(output_path), new_filename)
|
||||
# Save the image to the final output path
|
||||
canvas.save(filename=final_output_path)
|
||||
print(f"Saved to: {final_output_path}")
|
||||
set_canvas_size(900, 900)
|
||||
Reference in New Issue
Block a user