Skip to content

Instantly share code, notes, and snippets.

@rdenadai
Last active November 25, 2015 15:06
Show Gist options
  • Save rdenadai/58015099488449e5ed87 to your computer and use it in GitHub Desktop.
Save rdenadai/58015099488449e5ed87 to your computer and use it in GitHub Desktop.
A simple command line python script which convert to grayscale and crop some image multiple times with the same size.
import sys, getopt
import argparse
from PIL import Image
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--image')
parser.add_argument('-o', '--output')
parser.add_argument('-d', '--divisor')
args = parser.parse_args()
try:
divisor = int(args.divisor) if args.divisor else 4
except:
divisor = 4
if args.image and args.output:
with Image.open(args.image).convert('L') as im:
w = im.width / divisor
i = 1
left = 0
top = 0
right = w
bottom = im.height
while i <= divisor:
im.crop((left, top, right, bottom)).save("%s%s.jpg" % (args.output, i,))
left += w
right += w
i += 1
else:
parser.print_help()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment