Skip to content

Instantly share code, notes, and snippets.

@mstevenson
Created November 8, 2022 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mstevenson/191aa7934db9704f4ca78f6c45f0e0b6 to your computer and use it in GitHub Desktop.
Save mstevenson/191aa7934db9704f4ca78f6c45f0e0b6 to your computer and use it in GitHub Desktop.
Stable Diffusion training image cropping utility
import os
from PIL import Image
path = 'input_image_dir_path'
save_path = 'output_image_dir_path'
for filename in os.listdir(path):
img = Image.open(path + filename)
if img is not None:
width, height = img.size
if width > height:
left = (width - height) / 2
right = (width + height) / 2
top = 0
bottom = height
else:
left = 0
right = width
top = (height - width) / 2
bottom = (height + width) / 2
img = img.crop((left, top, right, bottom))
img = img.resize((512, 512), Image.LANCZOS)
img.save(save_path + filename, 'PNG')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment