Update files
This commit is contained in:
@@ -1,69 +1,150 @@
|
||||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
from tkinter import filedialog, messagebox
|
||||
from utils.image_processing import resize_image
|
||||
from utils.image_processing import ImageProcessor
|
||||
|
||||
selected_directory = ""
|
||||
|
||||
def browse_directory():
|
||||
global selected_directory
|
||||
selected_directory = filedialog.askdirectory()
|
||||
return selected_directory
|
||||
class FileProcessor:
|
||||
"""
|
||||
Class to handle file processing operations.
|
||||
"""
|
||||
|
||||
def get_first_image_path():
|
||||
if not selected_directory:
|
||||
def __init__(self):
|
||||
self.selected_directory = ""
|
||||
|
||||
def browse_directory(self):
|
||||
"""
|
||||
Open a dialog to select a directory.
|
||||
|
||||
Returns:
|
||||
str: The selected directory path.
|
||||
"""
|
||||
self.selected_directory = filedialog.askdirectory()
|
||||
return self.selected_directory
|
||||
|
||||
def get_first_image_path(self):
|
||||
"""
|
||||
Get the path of the first image in the selected directory.
|
||||
|
||||
Returns:
|
||||
str: The path to the first image, or None if no images found.
|
||||
"""
|
||||
if not self.selected_directory:
|
||||
return None
|
||||
|
||||
for root, dirs, files in os.walk(self.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
|
||||
|
||||
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 log_message(self, message, log=None):
|
||||
"""
|
||||
Log a message or print it if no log function is provided.
|
||||
|
||||
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
|
||||
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)
|
||||
Args:
|
||||
message (str): The message to log or print.
|
||||
log (function, optional): The log function to use. Defaults to None.
|
||||
"""
|
||||
if log:
|
||||
log("Existing directory removed.")
|
||||
os.makedirs(output_directory, exist_ok=True)
|
||||
if log:
|
||||
log(f"Output directory created: {output_directory}")
|
||||
log(message)
|
||||
else:
|
||||
print(message)
|
||||
|
||||
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)}")
|
||||
def process_directory_with_logging(self, options):
|
||||
"""
|
||||
Process images in the selected directory with logging.
|
||||
|
||||
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)
|
||||
Args:
|
||||
options (dict): Processing options.
|
||||
"""
|
||||
if not self.selected_directory:
|
||||
messagebox.showwarning(
|
||||
"No Directory", "Please select a directory.")
|
||||
return
|
||||
log = options.get("log_message", None)
|
||||
self.log_message(
|
||||
f"Processing started for directory: {self.selected_directory}", log
|
||||
)
|
||||
|
||||
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}")
|
||||
output_directory = self.create_output_directory(log)
|
||||
image_paths = self.collect_image_paths(log)
|
||||
|
||||
messagebox.showinfo("Process Complete", "Image processing is complete.")
|
||||
if log:
|
||||
log("Processing complete.")
|
||||
self.process_images(image_paths, output_directory, options, log)
|
||||
|
||||
messagebox.showinfo("Process Complete",
|
||||
"Image processing is complete.")
|
||||
self.log_message("Processing complete.", log)
|
||||
|
||||
def create_output_directory(self, log):
|
||||
"""
|
||||
Create the output directory for processed images.
|
||||
|
||||
Args:
|
||||
log (function): The log function to use.
|
||||
|
||||
Returns:
|
||||
str: The path to the output directory.
|
||||
"""
|
||||
output_directory = os.path.join(
|
||||
self.selected_directory, "ProcessedImages")
|
||||
if os.path.exists(output_directory):
|
||||
shutil.rmtree(output_directory)
|
||||
self.log_message("Existing directory removed.", log)
|
||||
os.makedirs(output_directory, exist_ok=True)
|
||||
self.log_message(f"Output directory created: {output_directory}", log)
|
||||
return output_directory
|
||||
|
||||
def collect_image_paths(self, log):
|
||||
"""
|
||||
Collect all image paths in the selected directory.
|
||||
|
||||
Args:
|
||||
log (function): The log function to use.
|
||||
|
||||
Returns:
|
||||
list: A list of image paths.
|
||||
"""
|
||||
image_paths = []
|
||||
for root, dirs, files in os.walk(self.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)
|
||||
self.log_message(f"Found: {file_path}", log)
|
||||
self.log_message(f"Total images found: {len(image_paths)}", log)
|
||||
return image_paths
|
||||
|
||||
def process_images(self, image_paths, output_directory, options, log):
|
||||
"""
|
||||
Process each image by resizing and saving it to the output directory.
|
||||
|
||||
Args:
|
||||
image_paths (list): A list of image paths.
|
||||
output_directory (str): The path to the output directory.
|
||||
options (dict): Processing options.
|
||||
log (function): The log function to use.
|
||||
"""
|
||||
image = ImageProcessor()
|
||||
for file_path in image_paths:
|
||||
output_path = os.path.join(
|
||||
output_directory, os.path.relpath(
|
||||
file_path, self.selected_directory)
|
||||
)
|
||||
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
||||
image.resize_image(
|
||||
file_path, output_path, options.get("additional_name", "")
|
||||
)
|
||||
|
||||
if os.path.exists(file_path) and options.get("is_checked", False):
|
||||
self.log_message(f"Removing: {file_path}", log)
|
||||
os.remove(file_path)
|
||||
self.log_message(f"Processed: {file_path}", log)
|
||||
|
||||
@@ -2,33 +2,70 @@ import os
|
||||
from wand.image import Image
|
||||
from wand.color import Color
|
||||
|
||||
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):
|
||||
class ImageProcessor:
|
||||
def __init__(
|
||||
self, canvas_width=900, canvas_height=900, background_color="transparent"
|
||||
):
|
||||
"""
|
||||
Initialize the ImageProcessor with default values.
|
||||
"""
|
||||
self.canvas_width = canvas_width
|
||||
self.canvas_height = canvas_height
|
||||
self.background_color = background_color
|
||||
|
||||
# Normalize the paths to ensure consistency
|
||||
image_path = os.path.normpath(image_path)
|
||||
output_path = os.path.normpath(output_path)
|
||||
def set_canvas_size(self, width, height):
|
||||
"""
|
||||
Set the canvas size.
|
||||
"""
|
||||
self.canvas_width = int(width)
|
||||
self.canvas_height = int(height)
|
||||
|
||||
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)
|
||||
# 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]
|
||||
# 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)
|
||||
def set_background_color(self, color):
|
||||
"""
|
||||
Set the background color.
|
||||
"""
|
||||
self.background_color = Color(color)
|
||||
|
||||
def resize_image(self, image_path, output_path, additional_name=None):
|
||||
"""
|
||||
Resize and process the image.
|
||||
"""
|
||||
# Normalize the paths to ensure consistency
|
||||
image_path = os.path.normpath(image_path)
|
||||
output_path = os.path.normpath(output_path)
|
||||
print(image_path)
|
||||
print(output_path)
|
||||
with Image(filename=image_path) as img:
|
||||
img.transform(resize=f"{self.canvas_width}x{self.canvas_height}>")
|
||||
|
||||
x_offset = int((self.canvas_width - img.width) / 2)
|
||||
y_offset = int((self.canvas_height - img.height) / 2)
|
||||
|
||||
with Image(
|
||||
width=self.canvas_width,
|
||||
height=self.canvas_height,
|
||||
background=self.background_color,
|
||||
) 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]
|
||||
# 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}")
|
||||
|
||||
|
||||
# Example usage
|
||||
if __name__ == "__main__":
|
||||
processor = ImageProcessor()
|
||||
processor.set_canvas_size(900, 900)
|
||||
processor.set_background_color("white")
|
||||
processor.resize_image("input_image.jpg", "output_image.jpg", "example")
|
||||
|
||||
Reference in New Issue
Block a user