Skip to content

Instantly share code, notes, and snippets.

@Henelik
Created April 3, 2020 01:21
Show Gist options
  • Save Henelik/2b825a44595de2f7948a983516588ab0 to your computer and use it in GitHub Desktop.
Save Henelik/2b825a44595de2f7948a983516588ab0 to your computer and use it in GitHub Desktop.
A small script which takes an image sequence (e.g. an animation from Blender) and compiles it into a single spritesheet
import cv2
from os import listdir
from os.path import isfile, join
import numpy as np
class imageSequence():
def __init__(self):
self.images = []
def importImages(self, path):
files = [f for f in listdir(path) if isfile(join(path, f))]
for f in files:
self.importImage(path+'\\'+f)
def importImage(self, path):
image = cv2.imread(path, cv2.IMREAD_UNCHANGED)
self.images.append(image)
def generateSpriteSheet(self, sizeX, sizeY):
if len(self.images) == 0:
return None
sheetShape = list(self.images[0].shape)
sheetShape[0] *= sizeY
sheetShape[1] *= sizeX
sheet = np.zeros(sheetShape)
for i in range(len(self.images)):
im = self.images[i]
xIndex = i % sizeX
yIndex = i // sizeX
if yIndex >= sizeY:
# We still have images, but no more room on the sheet! Drop remaining images
print("Sheet is full. Dropping " + str(len(self.images)-i) + " images.")
return sheet
x1 = xIndex * im.shape[1]
x2 = (xIndex + 1) * im.shape[1]
y1 = yIndex * im.shape[0]
y2 = (yIndex + 1) * im.shape[0]
sheet[y1:y2,x1:x2,:] = im
return sheet
def writeSheet(self, sizeX, sizeY, path):
sheet = self.generateSpriteSheet(sizeX, sizeY)
print("Writing sheet to " + path)
cv2.imwrite(path, sheet)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment