I'm on windows 11, using a Python script to compress JPG images recursively in a folder and its subfolders. The script uses the Pillow library and is designed to reduce image quality by 70% while replacing the original files. However, I'm encountering an issue where the script suddenly stops processing in the middle of execution without any error message. Here's the script I'm using: import os from PIL import Image def compress_images(directory, quality): """ Recursively compresses all JPG images in a directory and its subdirectories. Args: directory (str): Path of the directory to process quality (int): Quality of the compressed image (1-100) """ # Convert path to absolute format directory = os.path.abspath(directory) # Counter for processed images processed = 0 # Iterate through all files in directory and subdirectories for root, dirs, files in os.walk(directory): for filename in files: # Check if file is a JPG image if filename.lower().endswith(('.jpg', '.jpeg')): filepath = os.path.join(root, filename) try: # Open the image with Image.open(filepath) as img: # Check if image is in RGB mode if img.mode in ('RGBA', 'P'): img = img.convert('RGB') # Save compressed image overwriting the original img.save(filepath, 'JPEG', quality=quality, optimize=True) processed += 1 print(f"Compressed: {filepath}") except Exception as e: print(f"Error processing {filepath}: {str(e)}") return processed # Directory path to process directory = r"C:\Users\TESTA LORENZO\Desktop\foto camion" # Calculate quality (30% of original = 70% reduction) quality = 30 # Run compression try: num_processed = compress_images(directory, quality) print(f"\nCompression completed!") print(f"Number of images processed: {num_processed}") except Exception as e: print(f"An error occurred: {str(e)}") The script works fine initially and processes some images, but then it just stops at a random point. No error messages are displayed nor useful logs, it just freezes and doesn't continue processing the remaining images. I tried adding more logs but no errors appear. It also doesn't use much memory while working Any idea why it might be stopping unexpectedly? Continue reading...