Skip to content

Simple image resize scripts #713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions image_resize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from PIL import Image
import os

folder = "assets/images"
new_size = (800, 600)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this force all images to be 800 x 600?

Is there a way to crop these to the correct ratio before resizing. My worry is that images would be warped or distorted.


for filename in os.listdir(folder):
if filename.lower().endswith(('.jpg', '.jpeg', '.png', '.webp')):
file_path = os.path.join(folder, filename)
original_size = os.path.getsize(file_path)

with Image.open(file_path) as img:
img.thumbnail(new_size)
img.save(file_path)

new_size_bytes = os.path.getsize(file_path)
reduction = original_size - new_size_bytes
percent = (reduction / original_size) * 100 if original_size else 0

print(f"βœ… Resized: {filename} | Saved {reduction//1024}KB ({percent:.1f}%)")
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ pytest-playwright
pytest-xprocess
axe-core-python==0.1.0
axe-playwright-python==0.1.4
Pillow==11.2.1

Loading