import os import imagehash from PIL import Image import shutil # Paths for your folders and Waldo image input_folder = "images" # Folder with images to check for Waldo output_folder = "found_waldo_images" # Folder to save images containing Waldo waldo_image_path = "symposium/waldo_transparent.png" # Path to your Waldo image # Create output folder if it doesn't exist os.makedirs(output_folder, exist_ok=True) # Load the Waldo image and compute its hash waldo_image = Image.open(waldo_image_path) waldo_hash = imagehash.phash(waldo_image) # Function to compare the hash of an image with Waldo's hash def detect_waldo_in_image(image_path): try: # Open image image = Image.open(image_path) # Compute the hash of the current image image_hash = imagehash.phash(image) # Compute the difference between the image hash and Waldo's hash hash_diff = waldo_hash - image_hash # If the difference is small (threshold), we consider it a match return hash_diff < 13 # You can adjust the threshold (10) based on your needs except Exception as e: print(f"Error processing {image_path}: {e}") return False # Process each image in the input folder for filename in os.listdir(input_folder): if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.tiff')): # Process common image formats image_path = os.path.join(input_folder, filename) # Check if the image matches the Waldo pattern if detect_waldo_in_image(image_path): print(f"Found Waldo in {filename}, saving to {output_folder}") # Copy the image to the output folder shutil.copy(image_path, os.path.join(output_folder, filename)) print("Processing complete. Images with Waldo have been saved.")