Created
July 5, 2020 19:16
-
-
Save shorinji/4b931e65fe308f532b439e23065056cc to your computer and use it in GitHub Desktop.
Takes a tileset image with possible spacing and offsets, and realigns it to a proper grid
This file contains hidden or 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
import pygame | |
import sys | |
from pygame.locals import * | |
TILE_PIXELS = 8 | |
tileGroups = [ | |
{ "x": 1, "y": 11, "tilesW": 16, "tilesH": 16, "offset": 0 }, | |
{ "x": 130, "y": 11, "tilesW": 16, "tilesH": 16, "offset": 0 }, | |
{ "x": 259, "y": 11, "tilesW": 16, "tilesH": 16, "offset": 0 }, | |
{ "x": 388, "y": 11, "tilesW": 16, "tilesH": 16, "offset": 0 }, | |
] | |
TILE_SIZE = TILE_PIXELS, TILE_PIXELS | |
inputFilename = "zelda_overworld.png" | |
outputFilename = "zelda_overworld_aligned.png" | |
pygame.init() | |
screen = pygame.display.set_mode([100, 100]) | |
inputSurface = pygame.image.load(inputFilename).convert() | |
outputWidth = 0 | |
outputHeight = 0 | |
for tile in tileGroups: | |
w = tile["tilesW"] * TILE_PIXELS | |
h = tile["tilesH"] * TILE_PIXELS | |
outputWidth += w | |
outputHeight = max(outputHeight, h) | |
print("Input size: %dx%d" % inputSurface.get_size()) | |
print("Output size: %dx%d" % (outputWidth, outputHeight)) | |
outputSurface = pygame.Surface((outputWidth, outputHeight), inputSurface.get_flags(), inputSurface) | |
outputTileIndex = 0 | |
outX = 0 | |
for tile in tileGroups: | |
outY = 0 | |
inputBaseX = tile["x"] | |
inputBaseY = tile["y"] | |
w = tile["tilesW"] | |
h = tile["tilesH"] | |
pixelWidth = w * TILE_PIXELS | |
for y in range(h): | |
for x in range(w): | |
left = inputBaseX + (x * TILE_PIXELS) | |
top = inputBaseY + (y * TILE_PIXELS) | |
source = inputSurface.subsurface(Rect((left, top), TILE_SIZE)) | |
outputBaseX = outputTileIndex * pixelWidth | |
dest = Rect(outputBaseX + (x * TILE_PIXELS), outY, TILE_PIXELS, TILE_PIXELS) | |
outputSurface.blit(source, dest) | |
outY += TILE_PIXELS | |
outX += pixelWidth | |
outputTileIndex += 1 | |
pygame.image.save(outputSurface, outputFilename) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment