Skip to content

Instantly share code, notes, and snippets.

@saamerm
Last active October 21, 2024 19:28
Show Gist options
  • Save saamerm/1f542f35dd477cf9de4a62ed416315ba to your computer and use it in GitHub Desktop.
Save saamerm/1f542f35dd477cf9de4a62ed416315ba to your computer and use it in GitHub Desktop.
Python Image Resizer AspectFit, using `brew install pillow`
import os
from PIL import Image
# Folder containing your images
folder_path = os.getcwd()
# Desired output size
target_size = (450, 180)
# Iterate over files in the folder
for filename in os.listdir(folder_path):
if 'Hero' in filename and filename.lower().endswith('.jpg'):
file_path = os.path.join(folder_path, filename)
# Open the image file
with Image.open(file_path) as img:
original_size = img.size
print(f"Processing image: {filename}")
print(f"Original size: {original_size}")
# Calculate the aspect ratio of the image
img_ratio = original_size[0] / original_size[1]
target_ratio = target_size[0] / target_size[1]
# Resize the image to ensure it fills the target size, but keep aspect ratio
if img_ratio > target_ratio:
# Wider than the target aspect ratio; resize based on height
new_height = target_size[1]
new_width = int(target_size[1] * img_ratio)
else:
# Taller than the target aspect ratio; resize based on width
new_width = target_size[0]
new_height = int(target_size[0] / img_ratio)
resized_img = img.resize((new_width, new_height), Image.LANCZOS)
# Now crop the resized image to exactly match the target size (centered)
left = (new_width - target_size[0]) / 2
top = (new_height - target_size[1]) / 2
right = left + target_size[0]
bottom = top + target_size[1]
# Crop to the target dimensions
cropped_img = resized_img.crop((left, top, right, bottom))
# Save the cropped and resized image with the same filename
cropped_img.save(os.path.join(folder_path, filename))
print(f"Image resized and saved as: {filename}\n")
# Desired output size
target_size = (220, 150)
# Iterate over files in the folder
for filename in os.listdir(folder_path):
if 'M' in filename and filename.lower().endswith('.jpg'):
file_path = os.path.join(folder_path, filename)
# Open the image file
with Image.open(file_path) as img:
original_size = img.size
print(f"Processing image: {filename}")
print(f"Original size: {original_size}")
# Calculate the aspect ratio of the image
img_ratio = original_size[0] / original_size[1]
target_ratio = target_size[0] / target_size[1]
# Resize the image to ensure it fills the target size, but keep aspect ratio
if img_ratio > target_ratio:
# Wider than the target aspect ratio; resize based on height
new_height = target_size[1]
new_width = int(target_size[1] * img_ratio)
else:
# Taller than the target aspect ratio; resize based on width
new_width = target_size[0]
new_height = int(target_size[0] / img_ratio)
resized_img = img.resize((new_width, new_height), Image.LANCZOS)
# Now crop the resized image to exactly match the target size (centered)
left = (new_width - target_size[0]) / 2
top = (new_height - target_size[1]) / 2
right = left + target_size[0]
bottom = top + target_size[1]
# Crop to the target dimensions
cropped_img = resized_img.crop((left, top, right, bottom))
# Save the cropped and resized image with the same filename
cropped_img.save(os.path.join(folder_path, filename))
print(f"Image resized and saved as: {filename}\n")
print("Resizing and cropping process complete.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment