Skip to content

Instantly share code, notes, and snippets.

@aarqon
Created February 25, 2017 05:24
Show Gist options
  • Save aarqon/70952dc387a56216fc2e4af49934e860 to your computer and use it in GitHub Desktop.
Save aarqon/70952dc387a56216fc2e4af49934e860 to your computer and use it in GitHub Desktop.
Slices an image into uniform square tiles of a given size
# Usage: python3 slicer.py [image] [size in pixels]
# Requires Pillow
import os
import sys
from PIL import Image
image_url = sys.argv[1]
size = int(sys.argv[2])
print(image_url, size)
image = Image.open(image_url)
image_name = os.path.splitext(image_url)[0]
if not os.path.isdir(image_name):
os.mkdir(image_name)
for x in range(0,image.width,16):
for y in range(0,image.height,16):
tile = image.crop((x,y,(x+16),(y+16)))
tile.save(image_name+'/'+str(x)+'-'+str(y)+'.png')
tile.close()
image.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment