Advertisement
Guest User

createSpriteSheet.py

a guest
Apr 6th, 2020
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. from PIL import Image
  2. import os, math, time
  3. max_frames_row = 10.0
  4. frames = []
  5. tile_width = 0
  6. tile_height = 0
  7.  
  8. spritesheet_width = 0
  9. spritesheet_height = 0
  10.  
  11. files = os.listdir("frames/")
  12. files.sort()
  13. print(files)
  14.  
  15. for current_file in files :
  16.     try:
  17.         with Image.open("frames/" + current_file) as im :
  18.             frames.append(im.getdata())
  19.     except:
  20.         print(current_file + " is not a valid image")
  21.  
  22. tile_width = frames[0].size[0]
  23. tile_height = frames[0].size[1]
  24.  
  25. if len(frames) > max_frames_row :
  26.     spritesheet_width = tile_width * max_frames_row
  27.     required_rows = math.ceil(len(frames)/max_frames_row)
  28.     spritesheet_height = tile_height * required_rows
  29. else:
  30.     spritesheet_width = tile_width*len(frames)
  31.     spritesheet_height = tile_height
  32.    
  33. print(spritesheet_height)
  34. print(spritesheet_width)
  35.  
  36. spritesheet = Image.new("RGBA",(int(spritesheet_width), int(spritesheet_height)))
  37.  
  38. for current_frame in frames :
  39.     top = tile_height * math.floor((frames.index(current_frame))/max_frames_row)
  40.     left = tile_width * (frames.index(current_frame) % max_frames_row)
  41.     bottom = top + tile_height
  42.     right = left + tile_width
  43.    
  44.     box = (left,top,right,bottom)
  45.     box = [int(i) for i in box]
  46.     cut_frame = current_frame.crop((0,0,tile_width,tile_height))
  47.    
  48.     spritesheet.paste(cut_frame, box)
  49.    
  50. spritesheet.save("spritesheet" + time.strftime("%Y-%m-%dT%H-%M-%S") + ".png", "PNG")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement