Resize a folder of images so that the maximum dimension is 512px (in width or height).
- Install Pillow:
pip install pillow
-
Put
resize.pyin your folder of images -
Run
python resize_images.py
Blammo
| import os | |
| from PIL import Image | |
| MAX_SIZE = 512 | |
| def resize_image(image_path): | |
| with Image.open(image_path) as img: | |
| img = img.convert("RGB") | |
| img.thumbnail((MAX_SIZE, MAX_SIZE), Image.LANCZOS) | |
| img.save(image_path) | |
| print(f"Resized {image_path}") | |
| def main(): | |
| for filename in os.listdir('.'): | |
| if filename.lower().endswith(('.jpg', '.jpeg', '.png')): | |
| resize_image(filename) | |
| if __name__ == "__main__": | |
| main() |