Fill the bottom-left corner of the overall base with flat sea
The AI-outpainted bottom-left corner of sources/overall/overall-square.png invented a coastline and harbour where there is only open sea. tools/sea_corner.py replaces a corner triangle (exactly the rotated source's alpha == 0 quadrant, so no real satellite pixel changes) with one flat colour sampled as the median of the real sea within 20 px of that edge (#111819 here). Applied to bottom_left and the overall tiles rebuilt (world.json version bump). Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
72fa9f5742
commit
6750cc8722
4 files changed
+121
-3
No files matched your search
@@ -74,6 +74,7 @@ tools/
|
||||
psd_tree.py inspect a PSD's layer tree
|
||||
export_base.py PSD -> build/<id>/base.png (base group only)
|
||||
release.py static-site ZIP + git tag + Gitea release with the ZIP attached
|
||||
sea_corner.py flat sea fill for an outpainted corner of the square base (bottom-left)
|
||||
tile.py base.png -> original and optional permanently trimmed tiles
|
||||
build.py orchestrator; also writes web/data/world.json
|
||||
preview.py stitch built tiles into one overview image
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,117 @@
|
||||
"""Fill outpainted corner triangles of the square base map with flat sea colour.
|
||||
|
||||
python tools/sea_corner.py # bottom_left, colour sampled from the sea next to it
|
||||
python tools/sea_corner.py bottom_left bottom_right
|
||||
python tools/sea_corner.py --color "#111819" # explicit colour
|
||||
python tools/sea_corner.py --dry-run # write build/sea-corner-preview.png only
|
||||
|
||||
tools/square_base.py rotates sources/overall/overall.jpeg by 45 degrees and
|
||||
outpaints the four empty corner triangles with AI imagery. Where a corner is
|
||||
open sea, invented coastline looks wrong; this replaces such a corner with one
|
||||
flat colour. The corner region is exactly the rotated source's alpha == 0
|
||||
area in that quadrant, so real satellite pixels are never touched. The colour
|
||||
defaults to the median of the real pixels within --band px of the corner's
|
||||
diamond edge, which is the sea the fill has to continue. Re-run after
|
||||
regenerating overall-square.png, then retile the base:
|
||||
|
||||
rm -rf web/tiles/overall && python tools/build.py overall
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
from PIL import Image, ImageFilter
|
||||
|
||||
from common import CONFIG, ROOT
|
||||
|
||||
CORNERS = ("top_left", "top_right", "bottom_left", "bottom_right")
|
||||
|
||||
|
||||
def corner_masks(source: Path, rotate: float, size: tuple[int, int]) -> tuple[np.ndarray, dict]:
|
||||
"""(alpha of the rotated source, {corner: boolean mask of its empty triangle})."""
|
||||
rotated = Image.open(source).convert("RGBA").rotate(rotate, expand=True, resample=Image.BICUBIC)
|
||||
if rotated.size != size:
|
||||
raise SystemExit(f"rotated source is {rotated.size}, square image is {size}; "
|
||||
"use the same --source/--rotate square_base.py used")
|
||||
alpha = np.array(rotated.getchannel("A"))
|
||||
h, w = alpha.shape
|
||||
yy, xx = np.mgrid[0:h, 0:w]
|
||||
empty = alpha == 0
|
||||
left, top = xx < w / 2, yy < h / 2
|
||||
masks = {
|
||||
"top_left": empty & left & top,
|
||||
"top_right": empty & ~left & top,
|
||||
"bottom_left": empty & left & ~top,
|
||||
"bottom_right": empty & ~left & ~top,
|
||||
}
|
||||
return alpha, masks
|
||||
|
||||
|
||||
def sample_colour(rgb: np.ndarray, alpha: np.ndarray, corner: np.ndarray, band: int) -> tuple[int, int, int]:
|
||||
"""Median of real pixels within `band` px of the corner's edge."""
|
||||
dilated = np.array(Image.fromarray((corner * 255).astype(np.uint8))
|
||||
.filter(ImageFilter.MaxFilter(2 * band + 1))) > 0
|
||||
selection = dilated & (alpha > 0)
|
||||
if not selection.any():
|
||||
raise SystemExit("no real pixels next to that corner to sample from")
|
||||
return tuple(int(v) for v in np.median(rgb[selection], axis=0))
|
||||
|
||||
|
||||
def parse_colour(value: str) -> tuple[int, int, int]:
|
||||
value = value.lstrip("#")
|
||||
if len(value) != 6:
|
||||
raise argparse.ArgumentTypeError("colour must be #rrggbb")
|
||||
return tuple(int(value[i:i + 2], 16) for i in (0, 2, 4))
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> None:
|
||||
parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
|
||||
parser.add_argument("corners", nargs="*", default=["bottom_left"], choices=CORNERS)
|
||||
parser.add_argument("--image", type=Path, help="square base image (default: maps.json world.image)")
|
||||
parser.add_argument("--source", type=Path, default=ROOT / "sources" / "overall" / "overall.jpeg",
|
||||
help="unrotated source the square image was made from")
|
||||
parser.add_argument("--rotate", type=float, default=45.0, help="degrees CCW square_base.py used")
|
||||
parser.add_argument("--color", type=parse_colour, help="fill colour #rrggbb (default: sampled)")
|
||||
parser.add_argument("--band", type=int, default=20, help="sampling band width in px")
|
||||
parser.add_argument("--noise", type=float, default=0.0,
|
||||
help="gaussian grain sigma to add to the fill (0 = perfectly flat)")
|
||||
parser.add_argument("--dry-run", action="store_true", help="write a preview, keep the image")
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
image_path = args.image
|
||||
if image_path is None:
|
||||
raw = json.loads(CONFIG.read_text(encoding="utf-8"))
|
||||
image_path = ROOT / raw["world"]["image"]
|
||||
image = Image.open(image_path).convert("RGB")
|
||||
rgb = np.array(image)
|
||||
alpha, masks = corner_masks(args.source, args.rotate, image.size)
|
||||
|
||||
out = rgb.copy()
|
||||
for corner in dict.fromkeys(args.corners):
|
||||
mask = masks[corner]
|
||||
colour = args.color or sample_colour(rgb, alpha, mask, args.band)
|
||||
fill = np.empty((int(mask.sum()), 3), dtype=np.float64)
|
||||
fill[:] = colour
|
||||
if args.noise > 0:
|
||||
fill += np.random.default_rng(0).normal(0, args.noise, fill.shape)
|
||||
out[mask] = np.clip(np.rint(fill), 0, 255).astype(np.uint8)
|
||||
print(f"{corner}: {int(mask.sum())} px -> #{colour[0]:02x}{colour[1]:02x}{colour[2]:02x}"
|
||||
+ (f" (+noise sigma {args.noise})" if args.noise > 0 else ""))
|
||||
|
||||
if args.dry_run:
|
||||
preview = ROOT / "build" / "sea-corner-preview.png"
|
||||
Image.fromarray(out).save(preview)
|
||||
print(f"preview written to {preview.relative_to(ROOT)}; {image_path.name} unchanged")
|
||||
return
|
||||
assert np.array_equal(out[alpha > 0], rgb[alpha > 0]), "real pixels must not change"
|
||||
Image.fromarray(out).save(image_path, optimize=True)
|
||||
print(f"wrote {image_path.relative_to(ROOT)}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
+1
-1
@@ -29,7 +29,7 @@
|
||||
"minZoom": 0,
|
||||
"maxNativeZoom": 3,
|
||||
"base": true,
|
||||
"version": "0050bb19",
|
||||
"version": "690d7d17",
|
||||
"trimmedVersion": null,
|
||||
"pxPerMeter": null,
|
||||
"trim": null
|
||||
|
||||
Reference in new issue
Block a user