Skip to content

Instantly share code, notes, and snippets.

@cassidoo
Created June 2, 2025 03:07
Show Gist options
  • Save cassidoo/2987607f7a708127ddc3a0aff3520e1b to your computer and use it in GitHub Desktop.
Save cassidoo/2987607f7a708127ddc3a0aff3520e1b to your computer and use it in GitHub Desktop.
Resize images so that their maximum dimension is 512 pixels
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()

Resize a folder of images so that the maximum dimension is 512px (in width or height).

How to use

  1. Install Pillow:
pip install pillow
  1. Put resize.py in your folder of images

  2. Run python resize_images.py

Blammo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment