Created
November 8, 2022 05:30
-
-
Save mstevenson/191aa7934db9704f4ca78f6c45f0e0b6 to your computer and use it in GitHub Desktop.
Stable Diffusion training image cropping utility
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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