Skip to content

Instantly share code, notes, and snippets.

@MichaelBelousov
Created February 23, 2024 20:25
Show Gist options
  • Save MichaelBelousov/6d916851c7d66a017eb4e4ab2ebcd555 to your computer and use it in GitHub Desktop.
Save MichaelBelousov/6d916851c7d66a017eb4e4ab2ebcd555 to your computer and use it in GitHub Desktop.
I don't remember. I think it deduped sprites. will read later
from PIL import Image
import tempfile
import os
from math import ceil, sqrt
# import argparse
imgpath = input('What is the path to the image? ')
img = Image.open(imgpath)
img_width = img.size[0]
img_height = img.size[1]
# xtiles = input('How many x tiles does the image have? ')
# ytiles = input('How many y tiles does the image have? ')
xchunk = int(input('What is the tile width? '))
ychunk = int(input('What is the tile height? '))
xtiles = img_width//xchunk
ytiles = img_height//ychunk
# xchunk = img_width//xtiles
# ychunk = img_height//ytiles
sprites = []
for i in (a*xchunk for a in range(xtiles)):
for j in (a*ychunk for a in range(ytiles)):
sprite = img.crop(box=(
i, j, i+xchunk, j+ychunk))
sprites.append(sprite)
for i, sprite in enumerate(sprites):
for j, other in enumerate(sprites[i+1:]):
if sprite == other:
sprites.remove(sprite)
print('removed duplicate')
compiled_tiles = ceil(sqrt(len(sprites)))
compiled_height = (ychunk and xchunk) * compiled_tiles
compiled_img = Image.new(
mode='RGBA',
size=(compiled_height,)*2,
color=(0, 0, 0, 1))
for i in (a*xchunk for a in range(compiled_tiles)):
for j in (a*ychunk for a in range(compiled_tiles)):
if not sprites:
break
sprite = sprites.pop()
compiled_img.paste(sprite, (i, j))
compiled_img.save('compiled.png')
print('saved in `compiled.png`')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment