Created
August 2, 2022 13:34
-
-
Save db0company/66d9f3b8e7b5191d0b6cf6abcf7461cf to your computer and use it in GitHub Desktop.
A script to crop a bunch of photo to match a given ratio. Requires imagemagick installed (convert) and pip install Pillow
This file contains 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 | |
import sys | |
import subprocess | |
from PIL import Image | |
ROTATE = True | |
CREATE_PAIRS = True | |
try: | |
folder = sys.argv[1] | |
ratio_x = float(sys.argv[2]) | |
ratio_y = float(sys.argv[3]) | |
except: | |
print('Args: folder x y (ratio)') | |
exit() | |
def call(command): | |
print(' ', command) | |
subprocess.call(command, shell=True) | |
call('mkdir -p {}/pngs'.format(folder)) | |
call('mkdir -p {}/cropped'.format(folder)) | |
filenames = [] | |
for filename in os.listdir(folder): | |
print(filename) | |
filename_without_extension = u'.'.join(filename.split('.')[:-1]) | |
if not filename_without_extension: | |
continue | |
filenames.append(filename_without_extension) | |
if os.path.exists('{}/cropped/{}.png'.format(folder, filename_without_extension)): | |
print(' Already cropped, skipped') | |
continue | |
call('convert "{}/{}" "{}/pngs/{}.png"'.format( | |
folder, filename, | |
folder, filename_without_extension, | |
)) | |
path = '{}/pngs/{}.png'.format(folder, filename_without_extension) | |
with Image.open(path) as image: | |
x, y = image.size | |
if (ROTATE | |
and ((ratio_x > ratio_y and x < y) | |
or (ratio_x < ratio_y and x > y))): | |
print(' Rotated 90deg') | |
call('convert "{}" -rotate 90 "{}"'.format(path, path)) | |
tmp = x | |
x = y | |
y = tmp | |
if x < y: | |
# ratio_x : ratio_y <-> x : ? | |
new_x = x | |
new_y = new_x * (ratio_y / ratio_x) | |
margin_x = 0 | |
margin_y = (y - new_y) / 2 | |
else: | |
# ratio_x : ratio_y <-> ? : y | |
new_y = y | |
new_x = new_y * (ratio_x / ratio_y) | |
margin_x = (new_x - x) / 2 | |
margin_y = 0 | |
print(' Size: ', x, y) | |
print(' New size: ', new_x, new_y) | |
print(' Margin: ', margin_x, margin_y) | |
new_path = path.replace('/pngs/', '/cropped/') | |
crop_command = 'convert "{}" -crop {}x{}+{}+{} "{}"'.format( | |
path, | |
int(new_x), int(new_y), | |
int(margin_x), int(margin_y), | |
new_path | |
) | |
print(' ', crop_command) | |
call(crop_command) | |
if CREATE_PAIRS: | |
print('Create pairs') | |
call('mkdir -p {}/pairs'.format(folder)) | |
loner = None | |
if len(filenames) % 2 > 0: | |
loner = filenames[-1] | |
filenames = filenames[:-1] | |
for image1, image2 in zip( | |
filenames[:int(len(filenames)/2)], | |
filenames[int(len(filenames)/2):], | |
): | |
with Image.open('{}/cropped/{}.png'.format(folder, image1)) as image: | |
x1, y1 = image.size | |
with Image.open('{}/cropped/{}.png'.format(folder, image2)) as image: | |
x2, y2 = image.size | |
if x1 > x2: | |
call('convert \( {}/cropped/{}.png -resize {}x \) {}/cropped/{}.png +append {}/pairs/{}{}.png'.format( | |
folder, image1, x2, | |
folder, image2, | |
folder, image1, image2, | |
)) | |
else: | |
call('convert {}/cropped/{}.png \( {}/cropped/{}.png -resize {}x \) +append {}/pairs/{}{}.png'.format( | |
folder, image1, | |
folder, image2, x1, | |
folder, image1, image2, | |
)) | |
if loner: | |
print((loner,)) | |
call('cp {}/cropped/{}.png {}/pairs/{}.png'.format( | |
folder, loner, | |
folder, loner, | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment