-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathspritesheet_padding.py
executable file
·57 lines (43 loc) · 2.07 KB
/
spritesheet_padding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
# super quick script to add padding between the tiles of a tileset, bleeding
# using the colour at the tile edges.
import sys
from PIL import Image
# get & process arguments
if len(sys.argv) < 4 or len(sys.argv) > 5:
print("Usage: padding.py <tileset file> <tile width> <tile height> [padding (default 1px)]")
sys.exit(1)
tileset = sys.argv[1]
tile_width = int(sys.argv[2])
tile_height = int(sys.argv[3])
padding = int(sys.argv[4]) if len(sys.argv) == 5 else 1
# load tileset, create some basic info
img = Image.open(tileset)
img_width, img_height = img.size
new_width = img_width + (img_width // tile_width) * padding * 2 # each tile gets padding on both sides
new_height = img_height + (img_height // tile_height) * padding * 2
new_img = Image.new("RGBA", (new_width, new_height), (0, 0, 0, 0))
# copy over the base existing tiles
for y in range(0, img_height, tile_height):
for x in range(0, img_width, tile_width):
tile = img.crop((x, y, x + tile_width, y + tile_height))
new_img.paste(tile, (x + ((x // tile_width) * 2 + 1) * padding, y + ((y // tile_height) * 2 + 1) * padding))
# copy the left & right edges of the tiles over enough times to fill the padding gap.
for x in range(0, new_width, tile_width + padding * 2):
for x1 in range(x + padding, x, -1):
bar = new_img.crop((x1, 0, x1 + 1, new_height))
new_img.paste(bar, (x1 - 1, 0))
for x in range(padding + tile_width - 1, new_width, tile_width + padding * 2):
for x1 in range(x, x + padding):
bar = new_img.crop((x1, 0, x1 + 1, new_height))
new_img.paste(bar, (x1 + 1, 0))
# same for top & bottom
for y in range(0, new_height, tile_height + padding * 2):
for y1 in range(y + padding, y, -1):
bar = new_img.crop((0, y1, new_width, y1 + 1))
new_img.paste(bar, (0, y1 - 1))
for y in range(padding + tile_height - 1, new_height, tile_height + padding * 2):
for y1 in range(y, y + padding):
bar = new_img.crop((0, y1, new_width, y1 + 1))
new_img.paste(bar, (0, y1 + 1))
new_img.save(tileset[:-4] + "_padded.png")