makethumbs.py (1555B)
1 from PIL import Image 2 import os 3 4 def create_thumbnail(image_path): 5 if '_thumb.jpeg' not in image_path: 6 try: 7 # Open the image 8 with Image.open(image_path) as img: 9 # Convert to RGB if necessary (handles RGBA, etc.) 10 11 # Create thumbnail filename (e.g., image.jpg -> image_thumb.jpg) 12 base, ext = os.path.splitext(image_path) 13 thumb_path = f"{base}_thumb{ext}" 14 basewidth = 100 15 wpercent = (basewidth/float(img.size[0])) 16 hsize = int((float(img.size[1])*float(wpercent))) 17 img = img.resize((basewidth,hsize), Image.ANTIALIAS) 18 # Save thumbnail 19 img.save(thumb_path) 20 print(f"Created thumbnail: {thumb_path}") 21 except Exception as e: 22 print(f"Error processing {image_path}: {e}") 23 24 def process_jpegs_in_folders(root_dir): 25 # Walk through all subfolders 26 for root, _, files in os.walk(root_dir): 27 for file in files: 28 # Check for JPEG files (case-insensitive) 29 if file.lower().endswith(('.jpg', '.jpeg')): 30 image_path = os.path.join(root, file) 31 create_thumbnail(image_path) 32 33 if __name__ == "__main__": 34 # Set the root directory to the current working directory 35 root_directory = os.getcwd() 36 print(f"Processing JPEG files in {root_directory} and its subfolders...") 37 process_jpegs_in_folders(root_directory) 38 print("Thumbnail creation complete!")