-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice.py
28 lines (22 loc) · 805 Bytes
/
slice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from PIL import Image, ImageFilter
from math import ceil
import sys
if len(sys.argv) != 3:
print("Usage slice image grid_size")
sys.exit(0)
original_img = sys.argv[1]
grid_size = int(sys.argv[2])
im = Image.open( original_img )
width, height = im.size
cell_count_horizontal = ceil(width/grid_size)
cell_count_vertical = ceil(height/grid_size)
for i in range(cell_count_horizontal):
for j in range(cell_count_vertical):
left = i*grid_size
upper = j*grid_size
right = left + grid_size
lower = upper + grid_size
region = im.crop((left, upper, right, lower))
new_img = Image.new('RGBA', (grid_size, grid_size) , (0, 0, 0, 255))
new_img.paste(region, (0, 0, grid_size, grid_size))
new_img.save("slice"+str(i)+"_"+str(j)+".png")