import os from PIL import Image from tkinter import Tk, filedialog def process_image(input_path, output_folder, quality=75): filename = os.path.basename(input_path) name, ext = os.path.splitext(filename) ext = ext.lower() # Definir nombre de salida (siempre .jpg) output_path = os.path.join(output_folder, f"{name}.jpg") with Image.open(input_path) as img: if ext in ['.jpg', '.jpeg']: # Solo comprimir JPEG img.save(output_path, format='JPEG', quality=quality, optimize=True) else: # Convertir a JPEG y comprimir img = img.convert("RGB") # Convertir de RGBA/PNG a RGB img.save(output_path, format='JPEG', quality=quality, optimize=True) print(f"Processed: {filename} → {output_path}") def compress_images_in_folder(folder_path, quality=75): output_folder = os.path.join(folder_path, "compressed_jpeg") os.makedirs(output_folder, exist_ok=True) for filename in os.listdir(folder_path): if filename.lower().endswith(('.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.webp')): input_path = os.path.join(folder_path, filename) process_image(input_path, output_folder, quality) print(f"\n✅ Finished. Compressed images saved in: {output_folder}") def choose_folder(): root = Tk() root.withdraw() return filedialog.askdirectory(title="Select folder with images to convert & compress") if __name__ == "__main__": folder = choose_folder() if folder: compress_images_in_folder(folder, quality=75) else: print("No folder selected.")
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter