Skip to content

Instantly share code, notes, and snippets.

@hoolrory
Last active January 18, 2016 12:29
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 hoolrory/ffa68547966cca3bae59 to your computer and use it in GitHub Desktop.
Save hoolrory/ffa68547966cca3bae59 to your computer and use it in GitHub Desktop.
# Python script to slice an image with a grid into equal sized pieces
# pip install Pillow
#
# Usage:
# python image_slicy.py IMAGE_PATH OUTPUT_FOLDER COLUMNS ROWS
import sys
from PIL import Image
image = sys.argv[1]
folder = sys.argv[2]
columns = int(sys.argv[3])
rows = int(sys.argv[4])
img = Image.open(image)
xSize = img.width / columns
ySize = img.height / rows
i = 0
for c in range(0, columns):
for r in range(0, rows):
x = c * xSize
y = r * ySize
sub_image = img.crop((x, y, x + xSize, y + ySize))
sub_image.save("{0}/img_{1}.png".format(folder, i))
i+= 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment