Files
dungeoncraft/art/generated/sprites/scripts/compose.py
T

31 lines
1.5 KiB
Python

# compose.py out.png raw:rows:rowcounts:index[:m[:scale]] ... - cut chosen figures out of raw sheets and
# set them in one row on flat magenta (feet on one line, wide gaps), for pixelize_sprite.py --views N
import sys
sys.path.insert(0, "/home/zink/Projects/dungeoncraft/tools")
import pixelize_sprite as ps
from PIL import Image, ImageOps
out = sys.argv[1]; figs = []
for spec in sys.argv[2:]:
parts = spec.split(":")
raw, rows, rc, idx = parts[0], int(parts[1]), [int(v) for v in parts[2].split(",")], int(parts[3])
img = Image.open(raw).convert("RGB")
mask, boxes = ps.figure_boxes(img, rows, max(rc), "auto", "auto", rc)
x0, y0, x1, y1 = boxes[idx]
rgba = img.crop((x0, y0, x1, y1)).convert("RGBA")
# background pixels transparent so they become pure magenta on the new sheet
a = Image.new("L", rgba.size, 0)
a.putdata([255 if mask[y][x] else 0 for y in range(y0, y1) for x in range(x0, x1)])
rgba.putalpha(a)
if len(parts) > 4 and parts[4] == "m":
rgba = ImageOps.mirror(rgba)
if len(parts) > 5:
k = float(parts[5])
rgba = rgba.resize((round(rgba.width * k), round(rgba.height * k)), Image.NEAREST)
figs.append(rgba)
gap = 120
W = sum(f.width for f in figs) + gap * (len(figs) + 1); H = max(f.height for f in figs) + 2 * gap
sheet = Image.new("RGBA", (W, H), (255, 0, 255, 255)); x = gap
for f in figs:
sheet.alpha_composite(f, (x, H - gap - f.height)); x += f.width + gap
sheet.convert("RGB").save(out); print(out, sheet.size)