init image

This commit is contained in:
2024-07-13 16:23:11 +02:00
commit 1183327e7e
13 changed files with 890 additions and 0 deletions

71
utils/file_operations.py Normal file
View File

@@ -0,0 +1,71 @@
import os
import shutil
import tempfile
from tkinter import filedialog, messagebox
from utils.image_processing import resize_image
selected_directory = ""
def browse_directory():
global selected_directory
selected_directory = filedialog.askdirectory()
return selected_directory
def get_first_image_path():
if not selected_directory:
return None
for root, dirs, files in os.walk(selected_directory):
if 'ProcessedImages' in dirs:
dirs.remove('ProcessedImages')
for file in files:
if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.webp', '.avif')):
return os.path.join(root, file)
return None
def process_directory_with_logging(additional_name, is_checked, log, update_previews):
if not selected_directory:
messagebox.showwarning("No Directory", "Please select a directory.")
return
if log:
log(f"Processing started for directory: {selected_directory}")
output_directory = os.path.join(selected_directory, 'ProcessedImages')
if os.path.exists(output_directory):
shutil.rmtree(output_directory)
if log:
log("Existing directory removed.")
os.makedirs(output_directory, exist_ok=True)
if log:
log(f"Output directory created: {output_directory}")
image_paths = []
for root, dirs, files in os.walk(selected_directory):
if 'ProcessedImages' in dirs:
dirs.remove('ProcessedImages')
for file in files:
if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.webp', '.avif')):
file_path = os.path.join(root, file)
image_paths.append(file_path)
if log:
log(f"Found: {file_path}")
if log:
log(f"Total images found: {len(image_paths)}")
for file_path in image_paths:
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:
os.remove(file_path)
if log:
log(f"Processed: {file_path}")
messagebox.showinfo("Process Complete", "Image processing is complete.")
if log:
log("Processing complete.")

28
utils/image_processing.py Normal file
View File

@@ -0,0 +1,28 @@
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
canvas_width = int(width)
canvas_height = int(height)
def resize_image(image_path, output_path, additional_name):
with Image(filename=image_path) as img:
img.transform(resize=f'{canvas_width}x{canvas_height}>')
x_offset = int((canvas_width - img.width) / 2)
y_offset = int((canvas_height - img.height) / 2)
with Image(width=canvas_width, height=canvas_height, background=Color('transparent')) as canvas:
canvas.composite(img, left=x_offset, top=y_offset)
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)