Take over the tools of the other projects
AquaNova, AquaNovaAndroid, Atlas Legends, TinyWold and XP-Battleship now use ComfyGen instead of their own copies. Added from them, with paths taken from the project's config: generate_mesh (Hunyuan3D), mesh_prep, trim, largest, uncut, level. generate.py defaults to the "prompts" module; generate_audio and level honour AUDIO_SUBDIR (Atlas Legends keeps its tracks in output/music). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
73
uncut.py
Normal file
73
uncut.py
Normal file
@@ -0,0 +1,73 @@
|
||||
"""Cuts a black backdrop out of a texture that was never background-removed.
|
||||
|
||||
Some art in assets/ui went in straight from the generator with its backdrop
|
||||
still on. On a panel drawn over the world that shows as a hard black rectangle
|
||||
round the painting - which is what map_sheet.png was doing: nought per cent
|
||||
transparent, forty-two per cent opaque black.
|
||||
|
||||
The backdrop is the dark region *connected to the border*, not every dark pixel:
|
||||
a compass rose has black in it and a shadow under a torn edge is meant to be
|
||||
there. Flood from the edge and take only what it reaches.
|
||||
|
||||
py -3.12 uncut.py ../assets/ui/map_sheet.png
|
||||
"""
|
||||
import sys
|
||||
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
from scipy import ndimage
|
||||
|
||||
# Dark enough to be backdrop rather than ink. The paintings here are lit
|
||||
# objects on black; anything this dark is not part of one.
|
||||
DARK = 42
|
||||
|
||||
# The edge between backdrop and painting is a gradient a few pixels wide.
|
||||
# Feathering it stops the cut showing as a hard fringe.
|
||||
FEATHER = 2
|
||||
|
||||
|
||||
def cut(path: str) -> None:
|
||||
image = Image.open(path).convert("RGBA")
|
||||
pixels = np.array(image)
|
||||
height, width = pixels.shape[:2]
|
||||
|
||||
dark = pixels[:, :, :3].mean(axis=2) < DARK
|
||||
|
||||
labels, count = ndimage.label(dark)
|
||||
if count == 0:
|
||||
print(f" {path}: nothing dark to cut")
|
||||
return
|
||||
|
||||
# Every component touching any edge of the image.
|
||||
border = set(labels[0, :]) | set(labels[-1, :]) | set(labels[:, 0]) | set(labels[:, -1])
|
||||
border.discard(0)
|
||||
if not border:
|
||||
print(f" {path}: no dark region reaches the edge - nothing to cut")
|
||||
return
|
||||
|
||||
backdrop = np.isin(labels, list(border))
|
||||
|
||||
alpha = pixels[:, :, 3].astype(np.float32)
|
||||
alpha[backdrop] = 0.0
|
||||
|
||||
# Soften the boundary: whatever is left near the cut loses some alpha too.
|
||||
near = ndimage.binary_dilation(backdrop, iterations=FEATHER) & ~backdrop
|
||||
alpha[near] *= 0.5
|
||||
|
||||
pixels[:, :, 3] = np.clip(alpha, 0, 255).astype(np.uint8)
|
||||
Image.fromarray(pixels).save(path)
|
||||
|
||||
print(f" {path}: cut {backdrop.mean() * 100:.1f}% to transparent")
|
||||
|
||||
|
||||
def main() -> int:
|
||||
if len(sys.argv) < 2:
|
||||
print(__doc__)
|
||||
return 1
|
||||
for path in sys.argv[1:]:
|
||||
cut(path)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user