|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import os import hashlib from PIL import Image def find_and_remove_duplicates(directory): hashes = {} duplicates_found = 0 files_processed = 0 print(f"Buscando duplicados en: {directory}") for filename in os.listdir(directory): filepath = os.path.join(directory, filename) # Skip directories and non-image files if os.path.isdir(filepath) or not filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')): print(f"Saltando '{filename}' (no es una imagen o es una carpeta).") continue files_processed += 1 try: with Image.open(filepath) as img: # Generate a hash of the image content img_hash = hashlib.md5(img.tobytes()).hexdigest() if img_hash in hashes: print(f"Se encontró un duplicado: '{filename}' es un duplicado de '{hashes[img_hash]}'.") os.remove(filepath) # Remove the duplicate duplicates_found += 1 else: hashes[img_hash] = filename # Store the first occurrence print(f"Procesado '{filename}'. Hash: {img_hash}") except Exception as e: print(f"Error al procesar '{filename}': {e}") print(f"\nProceso completado.") print(f"Archivos de imagen procesados: {files_processed}") print(f"Duplicados eliminados: {duplicates_found}") print(f"Imágenes únicas restantes: {len(hashes)}") # Define the folder where you want to check for duplicates image_folder = 'imagenes' # Call the function to find and remove duplicates find_and_remove_duplicates(image_folder) |

