make_tileable: seamless textures (half-shift cross-blend), both axes or x only

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 12:34:00 +01:00
parent fa2d0592fd
commit 0890bf025f

91
make_tileable.py Normal file
View File

@@ -0,0 +1,91 @@
"""Make a generated render tile seamlessly (textures for 3D floors, walls, cloth).
Takes the newest raw render for <name> from the project's output/ folder, cross-blends it
with a copy shifted by half its size (so the seams move into the middle and fade out),
scales it and writes ../Assets/Sprites/<name>.jpg (or .png with --png).
py -3.12 make_tileable.py tex_sand both directions (floors)
py -3.12 make_tileable.py --x tex_wall_stone only left/right (walls, friezes)
py -3.12 make_tileable.py --size 1024x512 tex_wall output size (default: render size)
py -3.12 make_tileable.py --blend 0.3 tex_sand width of the fade (share of the side, default 0.25)
Works on any image: make_tileable.py --file in.png out.jpg [--x] [--size WxH]
"""
import glob
import os
import sys
import numpy as np
from PIL import Image
def tileable(img, x_only=False, blend=0.25):
"""Seamless version of img: interior of the original, edges taken from the half-shifted copy."""
a = np.asarray(img.convert("RGB"), dtype=np.float32)
h, w, _ = a.shape
b = np.roll(a, (0 if x_only else h // 2, w // 2), axis=(0, 1))
def ramp(n):
# 0 at the edge, 1 after blend*n pixels, smooth in between
d = np.minimum(np.arange(n), np.arange(n)[::-1]) / max(1.0, blend * n)
d = np.clip(d, 0, 1)
return d * d * (3 - 2 * d)
wx = ramp(w)[None, :]
wy = np.ones((h, 1)) if x_only else ramp(h)[:, None]
k = (wx * wy)[..., None]
out = a * k + b * (1 - k)
return Image.fromarray(np.clip(out, 0, 255).astype(np.uint8))
def save(img, dest, size=None):
if size:
img = img.resize(size, Image.LANCZOS)
os.makedirs(os.path.dirname(dest), exist_ok=True)
if dest.lower().endswith(".png"):
img.save(dest, optimize=True)
else:
img.save(dest, quality=90)
print(f" -> {dest} {img.size[0]}x{img.size[1]}")
def main(args):
x_only = "--x" in args
png = "--png" in args
size, blend, files = None, 0.25, False
names = []
i = 0
while i < len(args):
a = args[i]
if a == "--size":
w, h = args[i + 1].lower().split("x"); size = (int(w), int(h)); i += 2; continue
if a == "--blend":
blend = float(args[i + 1]); i += 2; continue
if a == "--file":
files = True
elif not a.startswith("--"):
names.append(a)
i += 1
if files:
if len(names) != 2:
print(__doc__); return 1
save(tileable(Image.open(names[0]), x_only, blend), names[1], size)
return 0
if not names:
print(__doc__); return 1
import _bootstrap # noqa: F401 Projektordner (config.py) zuerst
import config
ok = True
for name in names:
raws = sorted(glob.glob(os.path.join(config.OUTPUT_DIR, f"{name}_raw_*.png")), key=os.path.getmtime)
if not raws:
print(f" {name}: no raw render in output/, skipped"); ok = False; continue
dest = os.path.join(config.ASSETS_DIR, "Sprites", name + (".png" if png else ".jpg"))
print(f" {name}: {os.path.basename(raws[-1])}")
save(tileable(Image.open(raws[-1]), x_only, blend), dest, size)
return 0 if ok else 1
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))