Skip to content

Instantly share code, notes, and snippets.

@hungte
Created July 9, 2023 08:22
Show Gist options
  • Save hungte/72c2af8789e428a916e4f3f6e9bb5ba2 to your computer and use it in GitHub Desktop.
Save hungte/72c2af8789e428a916e4f3f6e9bb5ba2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
import os
from PIL import Image
LRCROP = 600
def process(fpath):
im = Image.open(fpath)
crop_box = (LRCROP, 0, im.width-LRCROP, im.height - 1)
check_l = (0, 0, LRCROP, im.height / 3)
check_r = (im.width - LRCROP, 0, im.width - 1, im.height / 3)
colors_l = im.crop(check_l).getcolors()
colors_r = im.crop(check_r).getcolors()
if not colors_l or not colors_r:
print(f'Unable to check crop [{LRCROP}]: {fpath}') #, L check colors={colors_l}, R check colors={colors_r}')
return
if len(colors_l) != 1 or len(colors_r) != 1:
print(f'Unable to crop [{LRCROP}]: {fpath}') #, L check colors={colors_l}, R check colors={colors_r}')
return
im2 = im.crop(crop_box)
im2.save(fpath)
def main():
if len(sys.argv) < 2:
exit(f'Usage: {sys.argv[0]} FOLDER')
folder = sys.argv[1]
for root, dirs, files in os.walk(folder):
for name in files:
fpath = os.path.join(root, name)
if not fpath.endswith('.jpg'):
continue
process(fpath)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment