Skip to content
TheChatArchiveThe Chat Archive
o1-mini-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

Improve the stop button in the code. Currently it takes a long time from pressing the…

  1. User

    Improve the stop button in the code. Currently it takes a long time from pressing the button to stopping the process. Increase the speed at which the button acts. Python 3.12 Code: import ctypes import logging import os import shutil import subprocess import threading import time import tkinter as tk from collections import defaultdict from concurrent.futures import ThreadPoolExecutor, Future from contextlib import contextmanager from pathlib import Path from queue import Queue from tkinter import filedialog from tkinter import ttk from typing import Any, Callable, Optional, Sequence, Union, Generator, cast

    from dataclasses import dataclass, field

    import tempfile import numpy as np from PIL import Image

    --- Configuration ---

    @dataclass class SharedState: """Represents the shared state across the application."""

    processed_files_lock: threading.Lock = field(default_factory=threading.Lock)
    processed_files_count: int = 0
    total_files_count: int = 0
    log_message_queue: Queue[str] = field(default_factory=Queue)
    tab_messages: dict[str, list[str]] = field(
        default_factory=lambda: {"Log": [], "Black": [], "White": [], "Color": []}
    )
    stop_event: threading.Event = field(default_factory=threading.Event)
    

    class Config: """Configuration settings for the image processing application."""

    SCRIPT_DIR: Path = Path(__file__).parent.resolve()
    INPUT_FOLDER: Path = Path(os.getenv("INPUT_FOLDER", SCRIPT_DIR / "1. Input"))
    OUTPUT_FOLDER: Path = Path(os.getenv("OUTPUT_FOLDER", SCRIPT_DIR / "2. Output"))
    DEFAULT_INPUT_FOLDER: Path = INPUT_FOLDER
    DEFAULT_OUTPUT_FOLDER: Path = OUTPUT_FOLDER
    DEFAULT_RENAME_PATTERN: str = "{Filename}"
    
    # Executable Paths
    IMAGEMAGICK: Path = Path(
        os.getenv(
            "IMAGEMAGICK", "D:/Bilder/Python/0. Code/Requirements/ImageMagick.exe"
        )
    )
    FFMPEG: Path = Path(
        os.getenv("FFMPEG", "D:/Bilder/Python/0. Code/Requirements/ffmpeg.exe")
    )
    
    # Supported File Extensions
    AUDIO_EXTENSIONS: set[str] = {".mka", ".mpga", ".mp3", ".aac", ".flac", ".opus", ".ogg"}
    IMAGE_EXTENSIONS: set[str] = {".png", ".jpg", ".jpeg", ".webp"}
    VIDEO_EXTENSIONS: set[str] = {".mkv", ".mp4"}
    
    # Display and Formatting Constants
    SEPARATOR_LENGTH: int = 310
    PADDING_LENGTH: int = 82
    PROGRESS_BAR_LENGTH: int = 100
    
    # Default Resize Options
    DEFAULT_RESIZE_MODE: str = "Shortest Side"
    DEFAULT_RESIZE_WIDTH: int = 0
    DEFAULT_RESIZE_HEIGHT: int = 0
    DEFAULT_RESIZE_SIDE: int = 1600
    DEFAULT_RESIZE_FORMAT: str = "png"
    
    # Color Detection Thresholds
    COLORDIFF_THRESHOLD: int = 5
    FRACTION_COLORED_PIXELS_THRESHOLD: float = 0.03
    
    @classmethod
    def setup(cls) -> None:
        """Create output directory and configure logging settings."""
        cls.OUTPUT_FOLDER.mkdir(parents=True, exist_ok=True)
        logging.getLogger("PIL").setLevel(logging.WARNING)
    
    @classmethod
    def initialize(cls) -> SharedState:
        """Initialize configuration and return shared state."""
        cls.setup()
        return SharedState()
    

    @contextmanager def create_temp_directory() -> Generator[Path, None, None]: """Yield a temporary directory within the output directory.""" with tempfile.TemporaryDirectory(dir=Config.OUTPUT_FOLDER) as temp_dir: yield Path(temp_dir)

    --- Logging ---

    def log_error_messages(error_queue: Queue[tuple[Path, list[str]]]) -> None: """Log error messages from the error queue.""" error_dict: dict[Path, list[str]] = defaultdict(list) while not error_queue.empty(): file_path, error_messages = error_queue.get() error_dict[file_path].extend(error_messages)

    if error_dict:
        shared_state.log_message_queue.put("UPDATE_LOG")
        for file_path in sorted(error_dict.keys()):
            shared_state.log_message_queue.put(f"{file_path.resolve()}")
            shared_state.log_message_queue.put("")
            for error_message in sorted(error_dict[file_path]):
                log_error_message(error_message)
            shared_state.log_message_queue.put("")
    

    def log_and_execute_in_thread( choice: str, label: str, function: Callable[..., Any], *args: Any ) -> None: """Log the execution of a function and run it in a separate thread."""

    def run_in_thread() -> None:
        start_time = time.time()
        separator = "-" * Config.SEPARATOR_LENGTH
        shared_state.log_message_queue.put(f"{separator}\n{label}\n{separator}\n")
    
        try:
            total_files = function(*args)
        except FileNotFoundError as e:
            log_error_message(str(e))
            total_files = 0
        except (OSError, ValueError) as e:
            log_error_message(f"An error occurred during '{label}': {e}")
            total_files = 0
    
        end_time = time.time()
        execution_time = end_time - start_time
    
        shared_state.log_message_queue.put("")
        image_processor.collect_processing_results(Config.OUTPUT_FOLDER, choice)
    
        app.after(0, app.update_final_status, execution_time)
        app.after(0, app.update_text_area)
    
    thread = threading.Thread(target=run_in_thread)
    thread.start()
    

    --- Utility Functions ---

    def log_error_message(message: str) -> None: """Log an error message to the shared state queue.""" shared_state.log_message_queue.put(f"- {message}")

    def validate_and_load_image(path: Path) -> Optional[Image.Image]: """Validate and load image, return None if invalid.""" try: with Image.open(path) as img: match img.mode: case "P": return img.convert("L") case "L" | "RGB" | "RGBA": return img.copy() case _: return img.convert("RGB") except (OSError, ValueError) as e: log_error_message(f"Error reading image {path}: {e}") return None

    def load_image_to_numpy_array(path: Path, mode: Optional[str] = None) -> Optional[np.ndarray]: """Load an image and convert it to a NumPy array with error handling.""" img = validate_and_load_image(path) if img is None: return None if mode is not None: img = img.convert(mode) try: return np.array(img) except (OSError, ValueError) as e: log_error_message(f"Error converting image to array {path}: {e}") return None

    def convert_png_to_jpg(input_path: Path, output_path: Path) -> None: """Convert a PNG image to JPG format.""" try: with Image.open(input_path) as img: if img.mode not in ("RGB", "L"): img = img.convert("RGB")

            jpg_path = output_path.with_suffix(".jpg")
            img.save(jpg_path, format="JPEG", quality=80, subsampling=0)
    
        input_path.unlink()
    except Exception as e:
        log_error_message(f"Error converting {input_path} to JPG: {e}")
    

    def verify_executable_exists(executable: Path, name: str) -> None: """Check if a given executable is available in the system.""" if not shutil.which(str(executable)): raise FileNotFoundError(f"- {name} executable not found at: {executable}")

    def increment_processed_file_count(start_time: float, total_files: int) -> None: """Safely increment the processed files count and update the status.""" with shared_state.processed_files_lock: shared_state.processed_files_count += 1 update_processing_status(start_time, shared_state.processed_files_count, total_files)

    def generate_new_filename(pattern: str, original_filename: str) -> str: """Rename the file based on the provided pattern.""" base, ext = os.path.splitext(original_filename) new_base = pattern.replace("{Filename}", base).strip()

    # Ensure the new base name is not empty
    if not new_base:
        new_base = base
    
    # Replace or remove any illegal characters if necessary
    illegal_chars = r'\/:*?"<>|'
    for char in illegal_chars:
        new_base = new_base.replace(char, "_")
    
    return f"{new_base}{ext}"
    

    --- Process Bar and Multithreading ---

    def update_processing_status(start_time: float, processed_count: int, total_files: int) -> None: """Update the application's status text with processing details.""" elapsed_time = time.time() - start_time minutes, seconds = divmod(int(elapsed_time), 60) elapsed_time_str = f"{minutes:02d}:{seconds:02d}"

    start_time_str = time.strftime("%H:%M:%S", time.localtime(start_time))
    status_text = (
        f"Start Time: {start_time_str} - "
        f"Processed Files: {processed_count}/{total_files} - "
        f"Elapsed Time: {elapsed_time_str} Minutes"
    )
    
    padded_status_text = status_text.ljust(Config.PADDING_LENGTH)
    app.update_status_text(padded_status_text)
    

    def update_elapsed_time( start_time: float, total_files: int, stop_event: threading.Event ) -> None: """Continuously update the elapsed time until the stop event is set.""" while not stop_event.is_set(): update_processing_status(start_time, shared_state.processed_files_count, total_files) time.sleep(1)

    def start_elapsed_time_thread( start_time: float, total_files: int, stop_event: threading.Event ) -> threading.Thread: """Start a thread to update the elapsed time.""" thread = threading.Thread( target=update_elapsed_time, args=(start_time, total_files, stop_event), daemon=True, ) thread.start() return thread

    def execute_tasks_with_thread_pool( tasks: Sequence[Path], process_func: Callable[..., Any], *args: Any ) -> int: """Execute tasks using a thread pool, updating progress and handling errors.""" shared_state.processed_files_count = 0 shared_state.total_files_count = total = len(tasks) start_time = time.time()

    app.update_progress(0)
    
    stop_event = threading.Event()
    elapsed_time_thread = start_elapsed_time_thread(start_time, total, stop_event)
    
    try:
        with ThreadPoolExecutor() as executor:
            futures: list[Future] = [
                executor.submit(process_func, task, *args) for task in tasks
            ]
    
            for index, future in enumerate(futures, start=1):
                if shared_state.stop_event.is_set():
                    for f in futures:
                        f.cancel()
                    break
                try:
                    future.result()
                except Exception as e:
                    log_error_message(f"Error during processing: {e}")
                finally:
                    progress = (index / total) * 100
                    app.update_progress(progress)
    finally:
        stop_event.set()
        elapsed_time_thread.join()
    
    return total
    

    --- Color Detection And Adjustment ---

    def is_color_image( img: np.ndarray, diff_threshold: int = Config.COLORDIFF_THRESHOLD, fraction_threshold: float = Config.FRACTION_COLORED_PIXELS_THRESHOLD, ) -> bool: """Determine if an image is color based on RGB channel differences.""" # Check if the image has three channels (RGB) if img.ndim != 3 or img.shape[2] != 3: return False

    # Calculate the peak-to-peak (max - min) difference across the RGB channels for each pixel
    max_diff = np.ptp(img, axis=2)
    
    # Compute the fraction of pixels where the max difference exceeds the threshold
    fraction_colored = np.mean(max_diff > diff_threshold)
    
    # Determine if the image meets or exceeds the required fraction of colored pixels
    return fraction_colored >= fraction_threshold
    

    def adjust_grayscale_values(img: np.ndarray) -> tuple[np.ndarray, int, int]: """Adjust the values of a grayscale image.""" try: # Calculate modal white value in the range [250, 256) hist_white, bins_white = np.histogram(img[img >= 250], bins=6, range=(250, 256)) modal_white = int(bins_white[np.argmax(hist_white)])

        # Calculate modal black value in the range [0, 61)
        hist_black, bins_black = np.histogram(img[img < 61], bins=61, range=(0, 61))
        modal_black = int(bins_black[np.argmax(hist_black)])
    
        # Convert image to float for processing
        adjusted = img.astype(np.float64)
    
        # Create a mask for extreme original values (0 and 255)
        mask = (img == 0) | (img == 255)
    
        # Exclude extreme values from the white adjustment
        white_adjust_mask = ~mask
    
        # Scale image if modal white is not at maximum intensity
        if modal_white != 255:
            adjusted[white_adjust_mask] *= 255.0 / modal_white
            adjusted = np.clip(adjusted, 0, 255)
    
        # After white value adjustment, identify new extreme values
        new_extremes = (adjusted == 0) | (adjusted == 255)
    
        # Combine masks to exclude all extreme values from black adjustment
        total_extremes = mask | new_extremes
        black_adjust_mask = ~total_extremes
    
        # Adjust image based on modal black value
        if modal_black:
            adjusted[black_adjust_mask] = (
                adjusted[black_adjust_mask] - modal_black
            ) * (255.0 / (255 - modal_black))
            adjusted = np.clip(adjusted, 0, 255)
    
            gamma = 1.0 - (modal_black / 255.0)
            adjusted[black_adjust_mask] = (
                np.power(adjusted[black_adjust_mask] / 255.0, gamma) * 255
            )
    
        # Round to nearest integer and then convert to uint8
        adjusted = np.rint(adjusted).astype(np.uint8)
    
        return adjusted, modal_black, modal_white
    
    except ValueError as e:
        log_error_message(f"Error adjusting values for image: {e}")
        return img, 0, 255
    

    --- Image Analysis ---

    def analyze_image_color_properties(image_processor: "ImageProcessor") -> None: """Analyze images in the input folder for color properties.""" image_processor.reset_results() image_files: list[Path] = [ path for path in Config.INPUT_FOLDER.rglob("*") if path.is_file() and path.suffix.lower() in Config.IMAGE_EXTENSIONS ]

    if not image_files:
        log_error_message(f"No image files found in '{Config.INPUT_FOLDER}'")
        return
    
    with create_temp_directory() as temp_folder:
        start_time = time.time()
        execute_tasks_with_thread_pool(image_files, analyze_image_file, start_time, len(image_files))
    

    def analyze_image_file(path: Path, start_time: float, total: int) -> None: """Analyze an image to classify it as color or grayscale.""" img_array = load_image_to_numpy_array(path) if img_array is None: increment_processed_file_count(start_time, total) return

    subfolder = path.parent.relative_to(Config.INPUT_FOLDER).as_posix()
    filename = path.name
    
    if is_color_image(img_array):
        image_processor.color_results[subfolder].append(filename)
    else:
        # Load the image as grayscale before adjusting values
        img_gray = load_image_to_numpy_array(path, mode='L')
        if img_gray is None:
            increment_processed_file_count(start_time, total)
            return
        adjusted, modal_black, modal_white = adjust_grayscale_values(img_gray)
        if modal_black:
            image_processor.black_results[subfolder].append((filename, modal_black))
        if modal_white != 255:
            image_processor.white_results[subfolder].append((filename, modal_white))
    increment_processed_file_count(start_time, total)
    

    def identify_and_move_color_images(image_processor: "ImageProcessor") -> None: """Find and move color images to the output folder.""" image_processor.reset_results() image_extensions: set[str] = {ext.lower() for ext in Config.IMAGE_EXTENSIONS}

    tasks: list[Path]
    if not (
        tasks := [
            path
            for path in Config.INPUT_FOLDER.rglob("*")
            if path.is_file() and path.suffix.lower() in image_extensions
        ]
    ):
        log_error_message(f"No image files found in '{Config.INPUT_FOLDER}'")
        return
    
    with create_temp_directory():
        execute_tasks_with_thread_pool(
            tasks, move_color_image, time.time(), len(tasks), image_processor
        )
    

    def move_color_image( path: Path, start_time: float, total_files: int, image_processor: "ImageProcessor" ) -> None: """Move a color image file to the output folder with renaming and update processing status.""" img_array = load_image_to_numpy_array(path) if img_array is None: increment_processed_file_count(start_time, total_files) return

    if is_color_image(img_array):
        rel_path = path.relative_to(Config.INPUT_FOLDER)
        
        # Extract the original filename
        original_filename = rel_path.name
        
        # Apply renaming pattern to the original filename
        new_filename = generate_new_filename(app.RENAME_PATTERN.get(), original_filename)
        
        # Determine the new relative path with the renamed file
        new_rel_path = rel_path.with_name(new_filename)
        
        # Construct the full output path
        out_path = Config.OUTPUT_FOLDER / new_rel_path
        
        # Ensure that the parent directories exist
        out_path.parent.mkdir(parents=True, exist_ok=True)
        
        try:
            shutil.move(str(path), str(out_path))
        except Exception as e:
            log_error_message(f"Failed to move {path} to {out_path}: {e}")
            increment_processed_file_count(start_time, total_files)
            return
    
        subfolder = str(rel_path.parent)
        
        # **Store the original filename instead of the new filename**
        image_processor.color_results[subfolder].append(original_filename)
    
    increment_processed_file_count(start_time, total_files)
    

    --- Corrupted Files ---

    def check_for_corrupted_files() -> None: """Check for corrupted files in the input folder using FFmpeg.""" image_processor.reset_results()

    try:
        verify_executable_exists(Config.FFMPEG, "FFmpeg")
    except FileNotFoundError as error:
        log_error_message(str(error))
        return
    
    valid_extensions: set[str] = (
        Config.VIDEO_EXTENSIONS | Config.AUDIO_EXTENSIONS | Config.IMAGE_EXTENSIONS
    )
    tasks: list[Path] = [
        file
        for file in Config.INPUT_FOLDER.rglob("*")
        if file.is_file() and file.suffix.lower() in valid_extensions
    ]
    
    if not tasks:
        log_error_message(f"No valid media files found in '{Config.INPUT_FOLDER}'")
        return
    
    error_queue: Queue[tuple[Path, list[str]]] = Queue()
    start_time: float = time.time()
    total_files: int = len(tasks)
    
    def check_file(file_path: Path) -> None:
        try:
            base_command: list[str] = [
                str(Config.FFMPEG),
                "-v",
                "error",
                "-i",
                str(file_path),
            ]
            if file_path.suffix.lower() in Config.VIDEO_EXTENSIONS:
                base_command += ["-map", "0:a:0"]
            base_command += ["-f", "null", "-"]
    
            result: subprocess.CompletedProcess = subprocess.run(
                base_command,
                capture_output=True,
                text=True,
                encoding="utf-8",
                errors="ignore",
            )
            combined_output: str = f"{result.stdout}\n{result.stderr}"
            error_messages: list[str] = [
                line.strip()
                for line in combined_output.splitlines()
                if "error" in line.lower()
            ]
    
            if error_messages:
                error_queue.put((file_path.resolve(), error_messages))
        except Exception as e:
            error_queue.put(
                (file_path.resolve(), [f"Error processing file {file_path}: {e}"])
            )
        finally:
            increment_processed_file_count(start_time, total_files)
    
    execute_tasks_with_thread_pool(tasks, check_file)
    log_error_messages(error_queue)
    

    --- Resize Images ---

    def resize_images( input_folder: Path, output_folder: Path, image_processor: "ImageProcessor" ) -> None: """Resize images in the input folder and save them to the output folder.""" try: verify_executable_exists(Config.IMAGEMAGICK, "ImageMagick") except FileNotFoundError as e: log_error_message(str(e)) return

    tasks: list[Path] = [
        input_path
        for input_path in input_folder.rglob("*")
        if input_path.is_file() and input_path.suffix.lower() in Config.IMAGE_EXTENSIONS
    ]
    
    if not tasks:
        log_error_message(f"No image files found in '{input_folder}'")
        return
    
    with create_temp_directory() as temp_folder:
        execute_tasks_with_thread_pool(
            tasks,
            process_file,
            time.time(),
            len(tasks),
            image_processor,
            temp_folder,
            True,
        )
    

    def resize_image(input_path: Path, output_path: Path) -> Path: """Resize an image using ImageMagick.""" output_path = output_path.with_suffix(".png")

    identify_command = [
        str(Config.IMAGEMAGICK),
        str(input_path),
        "-format",
        "%wx%h",
        "info:",
    ]
    try:
        result = subprocess.run(
            identify_command,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True,
            check=True,
        )
        current_width, current_height = map(int, result.stdout.strip().split("x"))
    except subprocess.CalledProcessError as e:
        log_error_message(f"ImageMagick identify failed for {input_path}: {e.stderr.strip()}")
        return output_path
    
    resize_mode = app.RESIZE_MODE.get()
    maintain_aspect = app.maintain_aspect_ratio.get()
    
    if resize_mode == "Fit":
        target_width = int(app.RESIZE_WIDTH.get() or 0)
        target_height = int(app.RESIZE_HEIGHT.get() or 0)
    
        if maintain_aspect:
            if target_width == 0 and target_height > 0:
                target_width = int(target_height * current_width / current_height)
            elif target_height == 0 and target_width > 0:
                target_height = int(target_width * current_height / current_width)
    
        resize_arg = f"{target_width}x{target_height}!"
    else:  # Shortest Side
        target_size = int(app.RESIZE_SIDE.get())
        resize_arg = f"{target_size}x{target_size}^"
    
    is_upscaling = (
        resize_mode == "Fit"
        and (target_width > current_width or target_height > current_height)
    ) or (
        resize_mode == "Shortest Side"
        and target_size > min(current_width, current_height)
    )
    
    command = [
        str(Config.IMAGEMAGICK),
        str(input_path),
    ]
    
    if is_upscaling:
        command += [
            "-filter",
            "LanczosSharp",
            "-distort",
            "Resize",
            resize_arg,
        ]
    else:
        command += [
            "-colorspace",
            "RGB",
            "-filter",
            "Lanczos2Sharp",
            "-resize",
            resize_arg,
            "-colorspace",
            "sRGB",
        ]
    
    command += ["-define", "png:compression-level=5", str(output_path)]
    
    try:
        result = subprocess.run(
            command,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True,
            check=True,
        )
        if result.stderr:
            log_error_message(f"ImageMagick: {result.stderr.strip()}")
    except subprocess.CalledProcessError as e:
        log_error_message(f"ImageMagick resize failed for {input_path}: {e.stderr.strip()}")
    
    return output_path
    

    --- Processing ---

    def process_file( path: Path, start_time: float, total_files: int, image_processor: "ImageProcessor", temp_folder: Path, resize: bool = False, auto_correct_and_resize: bool = False, ) -> None: """Process a single image file.""" if shared_state.stop_event.is_set(): return

    img_array = load_image_to_numpy_array(path)
    if img_array is None:
        increment_processed_file_count(start_time, total_files)
        return
    
    relative_path = path.relative_to(Config.INPUT_FOLDER)
    temp_output_path = temp_folder / relative_path.with_suffix(".png")
    temp_output_path.parent.mkdir(parents=True, exist_ok=True)
    
    if auto_correct_and_resize:
        # Adjust and save image, then resize
        intermediate_path, _, _ = adjust_and_save_image(
            img_array, path, image_processor, temp_folder
        )
        resized_path = resize_image(intermediate_path, temp_output_path)
    elif resize:
        # Only resize the original image
        resized_path = resize_image(path, temp_output_path)
    else:
        # Adjust and save image without resizing
        resized_path, _, _ = adjust_and_save_image(
            img_array, path, image_processor, temp_folder
        )
    
    # Determine the final output format
    output_format = app.RESIZE_FORMAT.get().lower()
    if output_format not in ["jpg", "png"]:
        output_format = "png"  # Fallback to PNG if an unsupported format is selected
    
    # Apply renaming pattern
    new_filename = generate_new_filename(app.RENAME_PATTERN.get(), relative_path.name)
    new_filename = os.path.splitext(new_filename)[0] + f".{output_format}"
    final_output_path = Config.OUTPUT_FOLDER / relative_path.parent / new_filename
    final_output_path.parent.mkdir(parents=True, exist_ok=True)
    
    # Save the final processed file
    if output_format == "jpg":
        convert_png_to_jpg(resized_path, final_output_path)
    else:
        shutil.move(str(resized_path), str(final_output_path))
    
    increment_processed_file_count(start_time, total_files)
    

    def process_folder( folder: Path, image_processor: "ImageProcessor", auto_correct_and_resize: bool = False, ) -> None: """Process all images in a folder.""" image_processor.reset_results()

    if auto_correct_and_resize:
        try:
            verify_executable_exists(Config.IMAGEMAGICK, "ImageMagick")
        except FileNotFoundError as e:
            log_error_message(str(e))
            return
    
    tasks: list[Path] = [
        path
        for path in folder.rglob("*")
        if path.is_file() and path.suffix.lower() in Config.IMAGE_EXTENSIONS
    ]
    
    if not tasks:
        log_error_message(f"No image files found in '{folder}'")
        return
    
    with create_temp_directory() as temp_folder:
        execute_tasks_with_thread_pool(
            tasks,
            process_file,
            time.time(),
            len(tasks),
            image_processor,
            temp_folder,
            False,
            auto_correct_and_resize,
        )
    

    def adjust_and_save_image( img: np.ndarray, path: Path, image_processor: "ImageProcessor", temp_folder: Path, diff_threshold: int = Config.COLORDIFF_THRESHOLD, fraction_threshold: float = Config.FRACTION_COLORED_PIXELS_THRESHOLD, ) -> tuple[Path, Optional[int], Optional[int]]: """Process an image, adjusting and saving depending on whether it is color or grayscale.""" try: is_color = is_color_image(img, diff_threshold, fraction_threshold) relative_parent = path.parent.relative_to(Config.INPUT_FOLDER) out_path = temp_folder / relative_parent / f"{path.stem}.png" out_path.parent.mkdir(parents=True, exist_ok=True) subfolder = relative_parent.as_posix() filename = path.name

        if is_color:
            # Save color image as PNG
            Image.fromarray(img).save(out_path, format="PNG", compress_level=5)
            image_processor.color_results[subfolder].append(filename)
            return out_path, None, None
    
        # Load the image as grayscale before adjusting values
        img_gray = load_image_to_numpy_array(path, mode='L')
        if img_gray is None:
            log_error_message(f"Error loading image {path} as grayscale.")
            return out_path, None, None
    
        adjusted_img, modal_black, modal_white = adjust_grayscale_values(img_gray)
        Image.fromarray(adjusted_img).convert("L").save(
            out_path, format="PNG", compress_level=5
        )
    
        if modal_black:
            image_processor.black_results[subfolder].append((filename, modal_black))
        if modal_white != 255:
            image_processor.white_results[subfolder].append((filename, modal_white))
    
        return out_path, modal_black, modal_white
    
    except (OSError, ValueError) as e:
        log_error_message(f"Error processing image {path}: {e}")
        return path, None, None
    

    --- ImageProcessor Class ---

    class ImageProcessor: """Class to handle image processing results."""

    def __init__(self) -> None:
        self.white_results: dict[str, list[tuple[str, int]]] = defaultdict(list)
        self.black_results: dict[str, list[tuple[str, int]]] = defaultdict(list)
        self.color_results: dict[str, list[str]] = defaultdict(list)
    
    def collect_processing_results(self, output_folder: Path, choice: str) -> None:
        """Collect processing results and prepare them for display or logging."""
        results_map: dict[str, Union[tuple[str, str, str], tuple[str, str]]] = {
            "2": ("2. Analyze Images", "Modal Value Black", "Modal Value White"),
            "3": ("3. Find Color Images", "Filename"),
            "5": (
                "5. Automatic Color Correction",
                "Modal Value Black",
                "Modal Value White",
            ),
            "6": (
                "6. Automatic Color Correction + Resize",
                "Modal Value Black",
                "Modal Value White",
            ),
        }
        headers = results_map.get(choice)
        if headers:
            if choice == "3":
                self._write_results("Color", self.color_results, headers[0], headers[1])
            else:
                self._write_results("Black", self.black_results, headers[0], headers[1])
                self._write_results("White", self.white_results, headers[0], headers[2])
                self._write_results("Color", self.color_results, headers[0], "Filename")
    
    def _write_results(
        self,
        file_path: str,
        results: dict[str, Union[list[tuple[str, int]], list[str]]],
        header: str,
        value_name: str,
    ) -> None:
        """Write results to a specific file."""
        separator = "-" * Config.SEPARATOR_LENGTH
        content = f"{separator}\n{header}\n{separator}\n"
        if results:
            for subfolder, subfolder_results in sorted(results.items()):
                full_subfolder_path = Config.INPUT_FOLDER / subfolder
                content += f"\n{full_subfolder_path}\n\n"
                if value_name == "Filename":
                    subfolder_results = cast(list[str], subfolder_results)
                    subfolder_results.sort()
                    content += "\n".join(
                        f" - {filename}" for filename in subfolder_results
                    )
                else:
                    subfolder_results = cast(list[tuple[str, int]], subfolder_results)
                    subfolder_results.sort(key=lambda x: x[1], reverse=True)
                    content += "\n".join(
                        f" - {filename}, {value_name}: {value}"
                        for filename, value in subfolder_results
                    )
                content += "\n"
        shared_state.tab_messages[file_path].append(content)
    
    def reset_results(self) -> None:
        """Reset all results."""
        self.white_results = defaultdict(list)
        self.black_results = defaultdict(list)
        self.color_results = defaultdict(list)
    

    --- Tkinter App ---

    class App(tk.Tk): def init(self) -> None: super().init() self.setup_window() self.create_variables() self.create_widgets() self.configure_logging() self.process_start_time: float = 0.0

    def setup_window(self) -> None:
        self.configure_dpi_awareness()
        self.title("Image Processor")
        self.geometry("2100x1180")
        self.default_font = ("Roboto Flex", 16)
        self.apply_default_font()
    
    def configure_dpi_awareness(self) -> None:
        try:
            ctypes.windll.shcore.SetProcessDpiAwareness(1)
        except Exception as e:
            print(f"Could not set DPI awareness: {e}")
    
    def apply_default_font(self) -> None:
        self.option_add("*Font", self.default_font)
        self.style = ttk.Style()
        for widget_type in [
            "TButton",
            "TLabel",
            "TEntry",
            "TRadiobutton",
            "TNotebook.Tab",
        ]:
            self.style.configure(widget_type, font=self.default_font)
        self.style.configure("TFrame", background="#fdfdfd")
        self.style.configure("TLabel", background="#fdfdfd")
        self.style.configure("TNotebook", background="#f8f8f8")
        self.style.configure("TRadiobutton", background="#fdfdfd")
    
    def create_variables(self) -> None:
        self.RESIZE_MODE = tk.StringVar(value=Config.DEFAULT_RESIZE_MODE)
        self.RESIZE_WIDTH = tk.StringVar(value=str(Config.DEFAULT_RESIZE_WIDTH))
        self.RESIZE_HEIGHT = tk.StringVar(value=str(Config.DEFAULT_RESIZE_HEIGHT))
        self.RESIZE_SIDE = tk.StringVar(value=str(Config.DEFAULT_RESIZE_SIDE))
        self.RESIZE_FORMAT = tk.StringVar(value=Config.DEFAULT_RESIZE_FORMAT)
        self.RENAME_PATTERN = tk.StringVar(value=Config.DEFAULT_RENAME_PATTERN)
        self.option_var = tk.StringVar(value="1")
        self.status_text = tk.StringVar(value="")
        self.maintain_aspect_ratio = tk.BooleanVar(value=False)
    
    def create_widgets(self) -> None:
        main_frame = ttk.Frame(self, padding="20")
        main_frame.pack(fill=tk.BOTH, expand=True)
    
        self.create_folder_selection_frames(main_frame)
        self.create_resize_options_frame(main_frame)
        self.create_options_and_start_frame(main_frame)
        self.create_text_frame(main_frame)
        self.create_progress_bar(main_frame)
        self.create_status_label(main_frame)
    
    def create_folder_selection_frames(self, parent: ttk.Frame) -> None:
        self.create_folder_frame(
            parent,
            "Input",
            Config.INPUT_FOLDER,
            self.set_default_input,
            self.select_input_folder,
        )
        self.create_folder_frame(
            parent,
            "Output",
            Config.OUTPUT_FOLDER,
            self.set_default_output,
            self.select_output_folder,
        )
    
    def create_folder_frame(
        self,
        parent: ttk.Frame,
        label: str,
        default_folder: Path,
        default_command: Callable[[], None],
        select_command: Callable[[], None],
    ) -> None:
        frame = ttk.Frame(parent)
        frame.pack(fill=tk.X, pady=(0, 10))
    
        ttk.Button(frame, text="Default", command=default_command, width=10).pack(
            side=tk.LEFT, padx=(0, 10)
        )
    
        entry = ttk.Entry(frame)
        entry.insert(0, str(default_folder))
        entry.pack(side=tk.LEFT, expand=True, fill=tk.X)
    
        ttk.Button(
            frame, text=f"Select {label} Folder", command=select_command, width=20
        ).pack(side=tk.LEFT, padx=(10, 0))
    
        setattr(self, f"{label.lower()}_entry", entry)
    
    def create_resize_options_frame(self, parent: ttk.Frame) -> None:
        resize_frame = ttk.Frame(parent)
        resize_frame.pack(fill=tk.X, pady=(0, 10))
    
        ttk.Button(
            resize_frame, text="Default", command=self.reset_resize_options, width=10
        ).pack(side=tk.LEFT, padx=(0, 10))
    
        self.create_resize_options(resize_frame)
    
        self.start_button = ttk.Button(
            resize_frame, text="Start", command=self.start_processing, width=20
        )
        self.start_button.pack(side=tk.RIGHT, padx=(10, 0))
    
    def create_resize_options(self, frame: ttk.Frame) -> None:
        # Format Label and Combobox
        ttk.Label(frame, text="Format:").pack(side=tk.LEFT, padx=(0, 5))
        ttk.Combobox(
            frame,
            textvariable=self.RESIZE_FORMAT,
            values=["png", "jpg"],
            state="readonly",
            width=4,
        ).pack(side=tk.LEFT, padx=(0, 10))
    
        # Resize Label and Combobox
        ttk.Label(frame, text="Resize:").pack(side=tk.LEFT, padx=(0, 5))
        mode_combo = ttk.Combobox(
            frame,
            textvariable=self.RESIZE_MODE,
            values=["Shortest Side", "Fit"],
            state="readonly",
            width=12,
        )
        mode_combo.pack(side=tk.LEFT, padx=(0, 10))
        mode_combo.bind("<<ComboboxSelected>>", self.update_resize_options)
    
        # Frame for Resize Option Fields (Width, Height, Side)
        self.resize_options_frame = ttk.Frame(frame)
        self.resize_options_frame.pack(side=tk.LEFT, fill=tk.Y)
    
        # Width and Height Widgets
        self.width_label = ttk.Label(self.resize_options_frame, text="Width:")
        self.width_entry = ttk.Entry(
            self.resize_options_frame, textvariable=self.RESIZE_WIDTH, width=5
        )
        self.height_label = ttk.Label(self.resize_options_frame, text="Height:")
        self.height_entry = ttk.Entry(
            self.resize_options_frame, textvariable=self.RESIZE_HEIGHT, width=5
        )
        self.maintain_aspect_ratio_label = ttk.Label(
            self.resize_options_frame, text="Maintain Aspect Ratio", font=self.default_font
        )
        self.maintain_aspect_ratio_checkbox = ttk.Checkbutton(
            self.resize_options_frame, variable=self.maintain_aspect_ratio
        )
    
        # Side Widgets
        self.side_label = ttk.Label(self.resize_options_frame, text="Side:")
        self.side_entry = ttk.Entry(
            self.resize_options_frame, textvariable=self.RESIZE_SIDE, width=5
        )
    
        # Initialize Resize Options Based on Default Mode
        self.update_resize_options()
    
        # Rename Frame (Packed Last to Occupy Remaining Space)
        rename_frame = ttk.Frame(frame)
        rename_frame.pack(side=tk.LEFT, fill=tk.X, expand=True)
    
        rename_label = ttk.Label(rename_frame, text="Rename:")
        rename_label.pack(side=tk.LEFT, padx=(0, 5))
    
        rename_entry = ttk.Entry(
            rename_frame,
            textvariable=self.RENAME_PATTERN,
        )
        rename_entry.pack(side=tk.LEFT, fill=tk.X, expand=True)
    
    def create_options_and_start_frame(self, parent: ttk.Frame) -> None:
        frame = ttk.Frame(parent)
        frame.pack(fill=tk.X, pady=(0, 10))
    
        self.create_options_frame(frame)
        self.create_stop_button(frame)
    
    def create_options_frame(self, parent: ttk.Frame) -> None:
        options_frame = ttk.Frame(parent)
        options_frame.pack(side=tk.LEFT, fill=tk.X, expand=True)
    
        options = [
            ("Check For Corrupted Files", "1"),
            ("Analyze Images", "2"),
            ("Find Color Images", "3"),
            ("Resize Images", "4"),
            ("Automatic Color Correction", "5"),
            ("Automatic Color Correction + Resize", "6"),
        ]
    
        for col, (text, value) in enumerate(options):
            ttk.Radiobutton(
                options_frame, text=text, variable=self.option_var, value=value
            ).grid(row=0, column=col, sticky="ew")
            options_frame.columnconfigure(col, weight=1)
    
    def create_stop_button(self, parent: ttk.Frame) -> None:
        self.stop_button = ttk.Button(
            parent,
            text="Stop",
            command=self.stop_processing,
            width=20,
            state=tk.DISABLED,
        )
        self.stop_button.pack(side=tk.RIGHT, padx=(10, 0))
    
    def create_text_frame(self, parent: ttk.Frame) -> None:
        text_frame = ttk.Frame(parent)
        text_frame.pack(fill=tk.BOTH, expand=True)
    
        self.notebook = ttk.Notebook(text_frame)
        self.notebook.pack(fill=tk.BOTH, expand=True)
    
        self.text_areas: dict[str, tk.Text] = {}
        for tab in ["Log", "Black", "White", "Color"]:
            frame = ttk.Frame(self.notebook)
            self.notebook.add(frame, text=tab)
            self.text_areas[tab] = self.create_text_area(frame)
    
    def create_text_area(self, parent: ttk.Frame) -> tk.Text:
        frame = ttk.Frame(parent)
        frame.pack(fill=tk.BOTH, expand=True)
    
        text_area = tk.Text(
            frame,
            wrap=tk.NONE,
            bg="#ffffff",
            state="disabled",
            padx=7,
            pady=7,
            fg="#000000",
            font=self.default_font,
        )
        scrollbar_y = ttk.Scrollbar(frame, command=text_area.yview)
        scrollbar_x = ttk.Scrollbar(frame, command=text_area.xview, orient="horizontal")
    
        scrollbar_y.pack(side=tk.RIGHT, fill=tk.Y)
        text_area.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
        scrollbar_x.pack(side=tk.BOTTOM, fill=tk.X)
    
        text_area.config(yscrollcommand=scrollbar_y.set, xscrollcommand=scrollbar_x.set)
        return text_area
    
    def create_progress_bar(self, parent: ttk.Frame) -> None:
        self.progress = ttk.Progressbar(
            parent, mode="determinate", maximum=Config.PROGRESS_BAR_LENGTH
        )
        self.progress.pack(pady=(30, 0), padx=20, fill="x")
        self.progress.config(value=0)
    
    def create_status_label(self, parent: ttk.Frame) -> None:
        ttk.Label(parent, textvariable=self.status_text).pack(pady=(10, 15))
    
    def configure_logging(self) -> None:
        self.queue_thread = threading.Thread(
            target=self.update_text_area_from_queue, daemon=True
        )
        self.queue_thread.start()
    
    def set_default_folder(self, entry: ttk.Entry, default_folder: Path) -> Path:
        entry.delete(0, tk.END)
        entry.insert(0, str(default_folder))
        return default_folder
    
    def set_default_input(self) -> None:
        Config.INPUT_FOLDER = self.set_default_folder(
            self.input_entry, Config.DEFAULT_INPUT_FOLDER
        )
    
    def set_default_output(self) -> None:
        Config.OUTPUT_FOLDER = self.set_default_folder(
            self.output_entry, Config.DEFAULT_OUTPUT_FOLDER
        )
    
    def select_folder(self, entry: ttk.Entry, config_attr: str) -> None:
        selected_folder = filedialog.askdirectory()
        if selected_folder:
            path = Path(selected_folder).resolve()
            entry.delete(0, tk.END)
            entry.insert(0, str(path))
            setattr(Config, config_attr, path)
    
    def select_input_folder(self) -> None:
        self.select_folder(self.input_entry, "INPUT_FOLDER")
    
    def select_output_folder(self) -> None:
        self.select_folder(self.output_entry, "OUTPUT_FOLDER")
    
    def update_status_text(self, text: str) -> None:
        self.status_text.set(text)
        self.update_idletasks()
    
    def update_progress(self, value: float) -> None:
        self.progress["value"] = min(value, Config.PROGRESS_BAR_LENGTH)
        self.update_idletasks()
    
    def update_text_area(self, content_key: Optional[str] = None) -> None:
        if content_key is not None:
            tab_keys = [content_key]
        else:
            tab_keys = self.text_areas.keys()
    
        for tab_key in tab_keys:
            text_area = self.text_areas[tab_key]
            text_area.configure(state="normal")
            text_area.delete("1.0", tk.END)
            content = "\n".join(shared_state.tab_messages[tab_key])
            text_area.insert(tk.END, content)
            text_area.configure(state="disabled")
    
    def start_processing(self) -> None:
        shared_state.stop_event.clear()
        self.start_button.config(state=tk.DISABLED)
        self.stop_button.config(state=tk.NORMAL)
        self.process_start_time = time.time()
        if hasattr(self, "stopped_by_user"):
            delattr(self, "stopped_by_user")
        choice = self.option_var.get()
    
        actions = {
            "1": ("1. Check For Corrupted Files", check_for_corrupted_files),
            "2": ("2. Analyze Images", analyze_image_color_properties, image_processor),
            "3": ("3. Find Color Images", identify_and_move_color_images, image_processor),
            "4": (
                "4. Resize Images",
                resize_images,
                Config.INPUT_FOLDER,
                Config.OUTPUT_FOLDER,
                image_processor,
            ),
            "5": (
                "5. Automatic Color Correction",
                process_folder,
                Config.INPUT_FOLDER,
                image_processor,
            ),
            "6": (
                "6. Automatic Color Correction + Resize",
                process_folder,
                Config.INPUT_FOLDER,
                image_processor,
                True,
            ),
        }
    
        label, func, *args = actions.get(choice, ("Unknown Action", lambda: None))
        log_and_execute_in_thread(choice, label, func, *args)
    
    def stop_processing(self) -> None:
        shared_state.stop_event.set()
        self.stopped_by_user = True
        self.stop_button.config(state=tk.DISABLED)
        self.start_button.config(state=tk.NORMAL)
    
    def update_resize_options(self, event: Optional[tk.Event] = None) -> None:
        mode = self.RESIZE_MODE.get()
        
        # Hide all resize option widgets initially
        self.width_label.pack_forget()
        self.width_entry.pack_forget()
        self.height_label.pack_forget()
        self.height_entry.pack_forget()
        self.maintain_aspect_ratio_label.pack_forget()
        self.maintain_aspect_ratio_checkbox.pack_forget()
        self.side_label.pack_forget()
        self.side_entry.pack_forget()
    
        if mode == "Fit":
            # Show Width and Height Widgets
            self.width_label.pack(side=tk.LEFT, padx=(0, 5), pady=2)
            self.width_entry.pack(side=tk.LEFT, padx=(0, 10), pady=2)
            self.height_label.pack(side=tk.LEFT, padx=(0, 5), pady=2)
            self.height_entry.pack(side=tk.LEFT, padx=(0, 10), pady=2)
            self.maintain_aspect_ratio_label.pack(side=tk.LEFT, padx=(10, 5), pady=2)
            self.maintain_aspect_ratio_checkbox.pack(side=tk.LEFT, padx=(0, 10), pady=2)
        elif mode == "Shortest Side":
            # Show Side Widget
            self.side_label.pack(side=tk.LEFT, padx=(0, 5), pady=2)
            self.side_entry.pack(side=tk.LEFT, padx=(0, 10), pady=2)
    
    def reset_resize_options(self) -> None:
        self.RESIZE_MODE.set(Config.DEFAULT_RESIZE_MODE)
        self.RESIZE_WIDTH.set(str(Config.DEFAULT_RESIZE_WIDTH))
        self.RESIZE_HEIGHT.set(str(Config.DEFAULT_RESIZE_HEIGHT))
        self.RESIZE_SIDE.set(str(Config.DEFAULT_RESIZE_SIDE))
        self.RESIZE_FORMAT.set(Config.DEFAULT_RESIZE_FORMAT)
        
        # Reset the renaming pattern to its default value
        self.RENAME_PATTERN.set(Config.DEFAULT_RENAME_PATTERN)
        
        self.update_resize_options()
    
    def update_text_area_from_queue(self) -> None:
        while True:
            if not shared_state.log_message_queue.empty():
                try:
                    item = shared_state.log_message_queue.get(block=True, timeout=0.1)
                    self.process_log_message(item)
                except:
                    pass
            else:
                time.sleep(0.1)
    
    def process_log_message(self, item: str) -> None:
        update_tabs = {
            "UPDATE_LOG": "Log",
            "UPDATE_BLACK": "Black",
            "UPDATE_WHITE": "White",
            "UPDATE_COLOR": "Color",
        }
        if item in update_tabs:
            self.after(0, self.update_text_area, update_tabs[item])
        else:
            self.update_tab_messages(item)
    
    def update_tab_messages(self, item: str) -> None:
        if "Modal Value Black" in item:
            shared_state.tab_messages["Black"].append(item)
        if "Modal Value White" in item:
            shared_state.tab_messages["White"].append(item)
        if "Filename" in item:
            shared_state.tab_messages["Color"].append(item)
        shared_state.tab_messages["Log"].append(item)
    
        for tab in ["Log", "Black", "White", "Color"]:
            self.after(0, self.update_text_area, tab)
    
    def update_final_status(self, execution_time: float) -> None:
        self.update_progress(100)
        end_time = time.time()
        start_time = self.process_start_time
        processed_count = shared_state.processed_files_count
        total_files = shared_state.total_files_count
    
        start_time_str = time.strftime("%H:%M:%S", time.localtime(start_time))
        elapsed_minutes = int(execution_time // 60)
        elapsed_seconds = int(execution_time % 60)
        elapsed_time_str = f"{elapsed_minutes:02d}:{elapsed_seconds:02d}"
    
        status_text = (
            f"Start Time: {start_time_str} - "
            f"Processed Files: {processed_count}/{total_files} - "
            f"Elapsed Time: {elapsed_time_str} Minutes"
        )
    
        if hasattr(self, "stopped_by_user") and self.stopped_by_user:
            status_text += " - Processing stopped by user"
            delattr(self, "stopped_by_user")
    
        padded_status_text = status_text.ljust(Config.PADDING_LENGTH)
        self.update_status_text(padded_status_text)
        self.reset_gui()
    
    def reset_gui(self) -> None:
        self.start_button.config(state=tk.NORMAL)
        self.stop_button.config(state=tk.DISABLED)
    

    --- Main Execution ---

    if name == "main": shared_state = Config.initialize() image_processor = ImageProcessor() app = App() app.mainloop()

  2. Assistant

    To enhance the responsiveness of the Stop button in your Python Tkinter application, it's essential to ensure that the application can promptly halt ongoing processes when the user requests it. The delay you're experiencing is likely due to the way the current implementation handles the stopping mechanism, especially when dealing with blocking operations like subprocess.run.

    Below, I'll outline the key areas for improvement and provide detailed code modifications to achieve a more responsive Stop button.


    1. Understanding the Current Stop Mechanism

    Currently, the application uses a shared_state.stop_event (a threading.Event) to signal when the Stop button is pressed. The execute_tasks_with_thread_pool function monitors this event and attempts to cancel ongoing tasks by invoking future.cancel(). However, this approach has limitations:

    • Blocking Operations: Functions like subprocess.run are blocking and do not periodically check the stop_event. Once a subprocess is initiated, the thread waits until it completes, regardless of the stop_event state.

    • Limited Checkpoints: The stop_event is only checked at the beginning of processing functions (process_file, analyze_image_file, move_color_image, etc.), which may not be frequent enough to achieve immediate stopping.


    2. Improving the Responsiveness of the Stop Button

    To make the Stop button more responsive, consider the following strategies:

    1. Use subprocess.Popen Instead of subprocess.run:

      • subprocess.Popen allows more granular control over subprocesses, including the ability to terminate them if needed.
      • This change will enable the application to kill ongoing subprocesses promptly when the stop_event is set.
    2. Introduce Periodic Checks for stop_event:

      • Modify long-running functions to periodically check the stop_event and exit gracefully if it's set.
      • This approach ensures that the application doesn't wait for the completion of lengthy tasks before stopping.
    3. Implement Timeouts in Subprocess Calls:

      • If switching to subprocess.Popen isn't feasible for all cases, adding timeouts to subprocess.run can prevent indefinite blocking.
      • However, using subprocess.Popen is generally more flexible and powerful for this purpose.
    4. Handle Task Cancellation More Effectively:

      • Rather than waiting for each task to complete in future.result(), consider processing tasks asynchronously and handling cancellations promptly.

    3. Detailed Code Modifications

    Below are the comprehensive code modifications to implement the above strategies effectively.

    a. Refactor Subprocess Calls Using subprocess.Popen

    Replace subprocess.run with subprocess.Popen to gain better control over subprocesses.

    Example Modification in check_file:

    import subprocess
    import signal
    
    def check_file(file_path: Path) -> None:
        if shared_state.stop_event.is_set():
            return
    
        try:
            base_command = [
                str(Config.FFMPEG),
                "-v",
                "error",
                "-i",
                str(file_path),
            ]
            if file_path.suffix.lower() in Config.VIDEO_EXTENSIONS:
                base_command += ["-map", "0:a:0"]
            base_command += ["-f", "null", "-"]
    
            process = subprocess.Popen(
                base_command,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                text=True,
                encoding="utf-8",
                errors="ignore",
                preexec_fn=os.setsid  # For POSIX systems; use creationflags for Windows
            )
    
            while True:
                if shared_state.stop_event.is_set():
                    # Terminate the subprocess
                    if os.name == 'nt':
                        process.terminate()
                    else:
                        os.killpg(os.getpgid(process.pid), signal.SIGTERM)
                    break
    
                retcode = process.poll()
                if retcode is not None:
                    break
                time.sleep(0.1)  # Sleep briefly to prevent busy waiting
    
            stdout, stderr = process.communicate()
    
            combined_output = f"{stdout}\n{stderr}"
            error_messages = [
                line.strip()
                for line in combined_output.splitlines()
                if "error" in line.lower()
            ]
    
            if error_messages:
                error_queue.put((file_path.resolve(), error_messages))
        except Exception as e:
            error_queue.put(
                (file_path.resolve(), [f"Error processing file {file_path}: {e}"])
            )
        finally:
            increment_processed_file_count(start_time, total_files)
    

    Key Points:

    • Process Group Termination: For POSIX systems, os.killpg is used to terminate the entire process group. For Windows, process.terminate() is sufficient.
    • Polling with Sleep: The loop periodically checks if the stop_event is set and polls the subprocess's state, allowing for graceful termination.

    Apply Similar Changes to All Subprocess Calls:

    Ensure that all functions utilizing subprocess.run are refactored to use subprocess.Popen with appropriate termination logic.

    b. Enhance Processing Functions to Check stop_event More Frequently

    Ensure that all processing functions check the stop_event at multiple points, especially before and after significant operations.

    Example Modification in process_file:

    def process_file(
        path: Path,
        start_time: float,
        total_files: int,
        image_processor: "ImageProcessor",
        temp_folder: Path,
        resize: bool = False,
        auto_correct_and_resize: bool = False,
    ) -> None:
        """Process a single image file."""
        if shared_state.stop_event.is_set():
            return
    
        # Load Image
        img_array = load_image_to_numpy_array(path)
        if img_array is None:
            increment_processed_file_count(start_time, total_files)
            return
    
        if shared_state.stop_event.is_set():
            return
    
        # Determine Relative Path
        relative_path = path.relative_to(Config.INPUT_FOLDER)
        temp_output_path = temp_folder / relative_path.with_suffix(".png")
        temp_output_path.parent.mkdir(parents=True, exist_ok=True)
    
        if auto_correct_and_resize:
            # Adjust and save image
            intermediate_path, _, _ = adjust_and_save_image(
                img_array, path, image_processor, temp_folder
            )
            if shared_state.stop_event.is_set():
                return
            # Resize
            resized_path = resize_image(intermediate_path, temp_output_path)
        elif resize:
            # Only resize the original image
            resized_path = resize_image(path, temp_output_path)
        else:
            # Adjust and save image without resizing
            resized_path, _, _ = adjust_and_save_image(
                img_array, path, image_processor, temp_folder
            )
    
        if shared_state.stop_event.is_set():
            return
    
        # Determine Output Format
        output_format = app.RESIZE_FORMAT.get().lower()
        if output_format not in ["jpg", "png"]:
            output_format = "png"  # Fallback to PNG if an unsupported format is selected
    
        # Apply Renaming Pattern
        new_filename = generate_new_filename(app.RENAME_PATTERN.get(), relative_path.name)
        new_filename = os.path.splitext(new_filename)[0] + f".{output_format}"
        final_output_path = Config.OUTPUT_FOLDER / relative_path.parent / new_filename
        final_output_path.parent.mkdir(parents=True, exist_ok=True)
    
        if shared_state.stop_event.is_set():
            return
    
        # Save the Final Processed File
        if output_format == "jpg":
            convert_png_to_jpg(resized_path, final_output_path)
        else:
            shutil.move(str(resized_path), str(final_output_path))
    
        increment_processed_file_count(start_time, total_files)
    

    Key Points:

    • Interleaved Checks: Adding if shared_state.stop_event.is_set(): return at multiple points ensures that the function can exit promptly if stopping is requested.
    • Early Exits: Before performing resource-intensive operations, the function checks if it should continue processing.

    Apply Similar Enhancements:

    Ensure that all processing functions (analyze_image_file, move_color_image, etc.) incorporate periodic checks for the stop_event.

    c. Modify execute_tasks_with_thread_pool to Handle Subprocess Termination

    Ensure that when the stop_event is set, all running subprocesses are properly terminated.

    Example Modification:

    def execute_tasks_with_thread_pool(
        tasks: Sequence[Path], process_func: Callable[..., Any], *args: Any
    ) -> int:
        """Execute tasks using a thread pool, updating progress and handling errors."""
        shared_state.processed_files_count = 0
        shared_state.total_files_count = total = len(tasks)
        start_time = time.time()
    
        app.update_progress(0)
    
        stop_event = threading.Event()
        elapsed_time_thread = start_elapsed_time_thread(start_time, total, stop_event)
    
        try:
            with ThreadPoolExecutor(max_workers=os.cpu_count() or 4) as executor:
                futures: list[Future] = [
                    executor.submit(process_func, task, *args) for task in tasks
                ]
    
                for index, future in enumerate(as_completed(futures), start=1):
                    if shared_state.stop_event.is_set():
                        # Attempt to terminate all running futures
                        for f in futures:
                            f.cancel()
                        break
                    try:
                        future.result(timeout=0.1)  # Non-blocking with timeout
                    except concurrent.futures.TimeoutError:
                        continue  # Move to the next future
                    except Exception as e:
                        log_error_message(f"Error during processing: {e}")
                    finally:
                        progress = (index / total) * 100
                        app.update_progress(progress)
        finally:
            stop_event.set()
            elapsed_time_thread.join()
    
        return total
    

    Key Points:

    • Using as_completed: This allows the function to handle futures as they complete rather than waiting for each one sequentially.
    • Timeout in future.result(): Setting a small timeout prevents the loop from blocking indefinitely on any single future.
    • Graceful Cancellation: When stop_event is set, the loop attempts to cancel all remaining futures and breaks out of the processing loop.

    Note: To use as_completed, ensure you import it:

    from concurrent.futures import as_completed
    

    d. Enhance the GUI to Reflect Stopped Subprocesses

    When stopping, it's beneficial to provide immediate feedback in the GUI about the termination of subprocesses.

    Example Enhancement in process_log_message:

    def process_log_message(self, item: str) -> None:
        update_tabs = {
            "UPDATE_LOG": "Log",
            "UPDATE_BLACK": "Black",
            "UPDATE_WHITE": "White",
            "UPDATE_COLOR": "Color",
        }
        if item in update_tabs:
            self.after(0, self.update_text_area, update_tabs[item])
        else:
            self.update_tab_messages(item)
        
        if shared_state.stop_event.is_set():
            self.after(0, self.update_final_status, time.time() - self.process_start_time)
    

    Key Points:

    • Immediate Update: The GUI updates as soon as the stop_event is detected, providing real-time feedback to the user.
    • Final Status Update: Upon stopping, the final status reflects that processing was halted by the user.

    4. Complete Example of Updated stop_processing Method

    Here's how the stop_processing method in the App class should look after modifications:

    def stop_processing(self) -> None:
        """Handle the Stop button press to halt processing."""
        shared_state.stop_event.set()
        self.stopped_by_user = True
        self.stop_button.config(state=tk.DISABLED)
        self.start_button.config(state=tk.NORMAL)
        self.update_status_text("Stopping processing, please wait...")
    

    Key Points:

    • Set the Stop Event: Signals all processing threads to stop.
    • Disable the Stop Button: Prevents multiple stop requests.
    • Enable the Start Button: Allows the user to restart processing if desired.
    • Update Status Text: Provides immediate feedback that stopping has been initiated.

    5. Additional Recommendations

    1. Thread Safety:

      • Ensure that all shared resources (shared_state, GUI elements) are accessed in a thread-safe manner to prevent race conditions.
    2. Exception Handling:

      • Robustly handle exceptions, especially when attempting to terminate subprocesses, to maintain application stability.
    3. User Feedback:

      • Clearly indicate in the GUI when processing is stopped, whether it's due to completion or user intervention.
    4. Testing:

      • Thoroughly test the updated stopping mechanism under various scenarios to ensure reliability and responsiveness.

    By implementing these modifications, your application should exhibit a significantly improved responsiveness to the Stop button, allowing users to halt processing operations promptly and efficiently.