Skip to content

Instantly share code, notes, and snippets.

@1wheel

1wheel/make_sprite.py

Last active Mar 27, 2019
Embed
What would you like to do?
#!/usr/bin/python
from PIL import Image
import math
from os import listdir
imgSize = 8192
# imgSize = 512*2
svgPath = '/Users/adampearce/Desktop/1wheel/datavizsociety/badges/svg/'
pngPath = '/Users/adampearce/Desktop/1wheel/datavizsociety/badges/png/'
svg_images = listdir(svgPath)
svg_images.sort()
# svg_images = svg_images[:32]
test_images = []
for i, img in enumerate(svg_images):
test_images.append(img.replace('svg', 'png'))
print(test_images[0])
# test_image_list = '/Users/adampearce/Desktop/1wheel/datavizsociety/badges/png' # path to the image list
# with open(test_image_list, 'r') as f:
# test_images = f.readlines()
# test_images = map(str.strip, test_images)
grid = int(math.sqrt(len(test_images))) + 1
image_height = int(imgSize / grid)
image_width = int(imgSize / grid)
print(image_width)
print(image_height)
big_image = Image.new(
mode='RGBA',
size=(image_width * grid, image_height * grid),
color=(0,0,0,0)) # fully transparent
for i in range(len(test_images)):
row = int(i / grid)
col = i % grid
img = Image.open(pngPath + test_images[i])
img = img.resize((image_height, image_width), Image.ANTIALIAS)
row_loc = row * image_height
col_loc = col * image_width
big_image.paste(img, (col_loc, row_loc)) # NOTE: the order is reverse due to PIL saving
big_image.save('../load-projector-data/sprite_image.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment