Created
June 7, 2023 09:17
-
-
Save alexandre97costa/a1d7ceae3833712eb84bab8b2c689dfb to your computer and use it in GitHub Desktop.
A python script to crop sprite 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
#!/usr/bin/env python3 | |
import os | |
from PIL import Image | |
IMG_PATH = 'dungeon-tiles-ii-extended/dungeontileset-extended.png' | |
TILES_PATH = 'tiles_list_v1.4' | |
def getBox(arr, frameIndex = 0): | |
w, h = int(arr[3]), int(arr[4]) | |
x, y = int(arr[1]) + w*frameIndex, int(arr[2]) | |
return (x, y, x+w, y+h) | |
def saveCrop(img, title, box): | |
path = os.path.join('./frames/' + title + '.png') | |
try: | |
croppedImg = img.crop(box) | |
croppedImg.save(path) | |
print('ok: ' + title) | |
except Exception as e: | |
print('fail: ' + title + ' -- ' + str(e)) | |
img = Image.open(IMG_PATH) | |
w, h = img.size | |
f = open(TILES_PATH, 'r') | |
for line in f.readlines(): | |
arr = line.split() | |
if len(arr) == 0: | |
pass | |
if len(arr) == 5: | |
saveCrop(img, arr[0], getBox(arr)) | |
if len(arr) == 6: | |
numOfFrames = int(arr[5]) | |
for frameIndex in range(0, numOfFrames): | |
saveCrop(img, arr[0] + '_f' + str(frameIndex), getBox(arr, frameIndex)) | |
print('') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment