Created
August 9, 2018 08:25
-
-
Save Mossman1215/cf8e035dbe40b3c07194d868d1a30748 to your computer and use it in GitHub Desktop.
create a spritesheet from a folder of images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#from https://minzkraut.com/2016/11/23/making-a-simple-spritesheet-generator-in-python/ | |
from PIL import Image | |
import os, math, time | |
max_frames_row = 16.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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment