Files
TarkovMaps/tools/place_preview.py

73 lines
2.5 KiB
Python

"""Render the rotated base map with a grid and every map's `place` box.
python tools/place_preview.py
Writes build/place-preview.png. This is the tool for positioning maps: read
coordinates straight off the grid, edit `place` in maps.json, re-run. No
tiling needed, so the loop is a couple of seconds.
Coordinates shown are ROTATED overall-map pixels, which is exactly what
`place.cx/cy/w` expect.
"""
from __future__ import annotations
from PIL import Image, ImageDraw
from common import BUILD, ROOT, load_config
Image.MAX_IMAGE_PIXELS = None
GRID = 100
def main() -> None:
cfg = load_config()
im = Image.open(cfg.base_image_path()).convert("RGB")
cfg.set_base_size(im.size)
if cfg.rotate:
im = im.rotate(cfg.rotate, expand=True, resample=Image.BICUBIC)
im = Image.eval(im, lambda v: min(255, int(v * 2.1))) # the art is very dark
w, h = im.size
d = ImageDraw.Draw(im)
for x in range(0, w, GRID):
d.line([x, 0, x, h], fill=(0, 90, 90))
d.text((x + 2, 2), str(x), fill=(0, 220, 220))
for y in range(0, h, GRID):
d.line([0, y, w, y], fill=(0, 90, 90))
d.text((2, y + 2), str(y), fill=(0, 220, 220))
for m in cfg.maps:
p = m.get("place")
if not p:
continue
if p.get("h") is not None:
aspect = p["h"] / p["w"]
else:
src = BUILD / m["id"] / "base.png"
aspect = 0.6
if src.exists():
with Image.open(src) as s:
cw, ch = (m["crop"][2] - m["crop"][0], m["crop"][3] - m["crop"][1]) \
if m.get("crop") else s.size
aspect = ch / cw
hw = p["w"] / 2.0
hh = hw * aspect
box = [p["cx"] - hw, p["cy"] - hh, p["cx"] + hw, p["cy"] + hh]
d.rectangle(box, outline=(255, 70, 70), width=3)
d.text((box[0] + 4, box[1] + 4), m["id"], fill=(255, 255, 0))
d.line([p["cx"] - 6, p["cy"], p["cx"] + 6, p["cy"]], fill=(255, 0, 0), width=2)
d.line([p["cx"], p["cy"] - 6, p["cx"], p["cy"] + 6], fill=(255, 0, 0), width=2)
d.text((10, h - 20), f"rotate={cfg.rotate}deg north up | rotated base {w}x{h}",
fill=(0, 255, 255))
out = BUILD / "place-preview.png"
out.parent.mkdir(parents=True, exist_ok=True)
im.save(out)
print(f"wrote {out.relative_to(ROOT)} {w}x{h}")
print("place boxes use these rotated coordinates; edit maps.json and re-run")
if __name__ == "__main__":
main()