-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcreateSpriteSheet.py
50 lines (39 loc) · 1.38 KB
/
createSpriteSheet.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from PIL import Image
import os, math, time
max_frames_row = 10.0
frames = []
tile_width = 0
tile_height = 0
spritesheet_width = 0
spritesheet_height = 0
files = os.listdir("frames/")
files.sort()
print(files)
for current_file in files :
try:
with Image.open("frames/" + current_file) as im :
frames.append(im.getdata())
except:
print(current_file + " is not a valid image")
tile_width = frames[0].size[0]
tile_height = frames[0].size[1]
if len(frames) > max_frames_row :
spritesheet_width = tile_width * max_frames_row
required_rows = math.ceil(len(frames)/max_frames_row)
spritesheet_height = tile_height * required_rows
else:
spritesheet_width = tile_width*len(frames)
spritesheet_height = tile_height
print(spritesheet_height)
print(spritesheet_width)
spritesheet = Image.new("RGBA",(int(spritesheet_width), int(spritesheet_height)))
for current_frame in frames :
top = tile_height * math.floor((frames.index(current_frame))/max_frames_row)
left = tile_width * (frames.index(current_frame) % max_frames_row)
bottom = top + tile_height
right = left + tile_width
box = (left,top,right,bottom)
box = [int(i) for i in box]
cut_frame = current_frame.crop((0,0,tile_width,tile_height))
spritesheet.paste(cut_frame, box)
spritesheet.save("spritesheet" + time.strftime("%Y-%m-%dT%H-%M-%S") + ".png", "PNG")