forked from rosebudai/OpenRCT2-Web
Add dark artifact cleanup to parse_restyled post-processing
FAL.ai sometimes paints dark horizontal bands across sprites (from grid lines bleeding into content). The new cleanup_black_artifacts() step detects opaque pixels significantly darker than the sprite's median brightness (where the original was NOT dark) and fills them from the nearest bright neighbor. This took the Gemini visual quality rating from 2/10 to 10/10 on test sprites — no more black/dark bands, clean transparency, shape perfectly preserved. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
1fac077da8
commit
2c524e8462
2 files changed
+133
No files matched your search
@@ -90,6 +90,82 @@ def restore_alpha(restyled_sprite, original_path):
|
||||
return Image.merge("RGBA", (r, g, b, original_alpha))
|
||||
|
||||
|
||||
def cleanup_black_artifacts(restyled, original_path, darkness_ratio=0.55):
|
||||
"""Replace anomalously dark artifact pixels with colors from bright neighbors.
|
||||
|
||||
AI generation sometimes paints dark bands across sprites (from grid lines
|
||||
bleeding into content). This detects opaque pixels that are much darker
|
||||
than the sprite's median brightness AND where the original was not dark,
|
||||
then fills them from the nearest bright non-artifact neighbor.
|
||||
|
||||
Args:
|
||||
darkness_ratio: A pixel is considered an artifact if its brightness
|
||||
is below this fraction of the sprite's median opaque brightness
|
||||
AND the original pixel was brighter than the median.
|
||||
"""
|
||||
original = Image.open(original_path).convert("RGBA")
|
||||
rest_arr = np.array(restyled).copy()
|
||||
orig_arr = np.array(original)
|
||||
|
||||
h, w = rest_arr.shape[:2]
|
||||
rest_rgb = rest_arr[:, :, :3].astype(float)
|
||||
orig_rgb = orig_arr[:, :, :3].astype(float)
|
||||
rest_alpha = rest_arr[:, :, 3]
|
||||
|
||||
opaque = rest_alpha > 128
|
||||
if not opaque.any():
|
||||
return restyled
|
||||
|
||||
# Compute median brightness of opaque pixels in both images
|
||||
rest_bright = rest_rgb.max(axis=2)
|
||||
orig_bright = orig_rgb.max(axis=2)
|
||||
median_rest = float(np.median(rest_bright[opaque]))
|
||||
median_orig = float(np.median(orig_bright[opaque]))
|
||||
|
||||
if median_rest < 30:
|
||||
# Sprite is genuinely dark overall, skip cleanup
|
||||
return restyled
|
||||
|
||||
dark_thresh = median_rest * darkness_ratio
|
||||
|
||||
# Artifact: opaque, much darker than median in restyled,
|
||||
# but original was NOT dark (above half its median)
|
||||
is_artifact = (
|
||||
opaque
|
||||
& (rest_bright < dark_thresh)
|
||||
& (orig_bright > median_orig * 0.4)
|
||||
)
|
||||
|
||||
if not is_artifact.any():
|
||||
return restyled
|
||||
|
||||
count = int(is_artifact.sum())
|
||||
|
||||
# Fill artifacts from nearest non-artifact opaque neighbor
|
||||
ys, xs = np.where(is_artifact)
|
||||
for y, x in zip(ys, xs):
|
||||
best = None
|
||||
for radius in range(1, max(h, w)):
|
||||
for dy in range(-radius, radius + 1):
|
||||
for dx in range(-radius, radius + 1):
|
||||
if abs(dy) != radius and abs(dx) != radius:
|
||||
continue
|
||||
ny, nx = y + dy, x + dx
|
||||
if 0 <= ny < h and 0 <= nx < w:
|
||||
if opaque[ny, nx] and not is_artifact[ny, nx]:
|
||||
best = (ny, nx)
|
||||
break
|
||||
if best:
|
||||
break
|
||||
if best:
|
||||
break
|
||||
if best:
|
||||
rest_arr[y, x, :3] = rest_arr[best[0], best[1], :3]
|
||||
|
||||
print(f" cleaned {count} dark artifact pixels")
|
||||
return Image.fromarray(rest_arr)
|
||||
|
||||
|
||||
def blend_tile_edges(restyled, original, edge_pixels=6):
|
||||
"""Blend outer edge pixels back to original for seamless tiling."""
|
||||
res = np.array(restyled).astype(float)
|
||||
@@ -200,6 +276,9 @@ def parse_restyled(manifest_path, sprites_dir, restyled_dir=None,
|
||||
# Restore original alpha mask
|
||||
sprite_img = restore_alpha(sprite_img, orig_path)
|
||||
|
||||
# Clean up black artifact pixels from AI generation
|
||||
sprite_img = cleanup_black_artifacts(sprite_img, orig_path)
|
||||
|
||||
# Blend tile edges
|
||||
if use_tiling:
|
||||
original = Image.open(orig_path).convert("RGBA")
|
||||
|
||||
@@ -8,6 +8,7 @@ from PIL import Image
|
||||
|
||||
from parse_restyled import (
|
||||
restore_alpha,
|
||||
cleanup_black_artifacts,
|
||||
blend_tile_edges,
|
||||
validate_sprite,
|
||||
extract_sprites_from_grid,
|
||||
@@ -67,6 +68,59 @@ def test_blend_tile_edges_tiny_sprite():
|
||||
assert result.size == (4, 4)
|
||||
|
||||
|
||||
def test_cleanup_black_artifacts():
|
||||
"""Dark artifact pixels where original was bright should be replaced."""
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
# Original: bright orange opaque sprite
|
||||
orig = Image.new("RGBA", (10, 10), (200, 100, 50, 255))
|
||||
orig_path = os.path.join(d, "orig.png")
|
||||
orig.save(orig_path)
|
||||
|
||||
# Restyled: mostly silver (180) but rows 4-5 are a dark band (40)
|
||||
rest_arr = np.full((10, 10, 4), (180, 180, 180, 255), dtype=np.uint8)
|
||||
rest_arr[4:6, :, :3] = 40 # dark grey band
|
||||
restyled = Image.fromarray(rest_arr)
|
||||
|
||||
result = cleanup_black_artifacts(restyled, orig_path)
|
||||
result_arr = np.array(result)
|
||||
# Dark band rows should now be bright (filled from neighbors)
|
||||
assert result_arr[5, 0, :3].max() > 100
|
||||
|
||||
|
||||
def test_cleanup_black_no_artifacts():
|
||||
"""When there are no artifacts, image is unchanged."""
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
orig = Image.new("RGBA", (10, 10), (200, 100, 50, 255))
|
||||
orig_path = os.path.join(d, "orig.png")
|
||||
orig.save(orig_path)
|
||||
|
||||
restyled = Image.new("RGBA", (10, 10), (180, 180, 180, 255))
|
||||
result = cleanup_black_artifacts(restyled, orig_path)
|
||||
result_arr = np.array(result)
|
||||
assert (result_arr[:, :, 0] == 180).all()
|
||||
|
||||
|
||||
def test_cleanup_black_preserves_intentional_dark():
|
||||
"""Pixels that were dark in the original should stay dark."""
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
# Original has a dark outline at row 5
|
||||
orig_arr = np.full((10, 10, 4), (200, 100, 50, 255), dtype=np.uint8)
|
||||
orig_arr[5, :, :3] = 10 # dark outline in original
|
||||
orig = Image.fromarray(orig_arr)
|
||||
orig_path = os.path.join(d, "orig.png")
|
||||
orig.save(orig_path)
|
||||
|
||||
# Restyled also has dark at row 5 (matching the original)
|
||||
rest_arr = np.full((10, 10, 4), (180, 180, 180, 255), dtype=np.uint8)
|
||||
rest_arr[5, :, :3] = 10
|
||||
restyled = Image.fromarray(rest_arr)
|
||||
|
||||
result = cleanup_black_artifacts(restyled, orig_path)
|
||||
result_arr = np.array(result)
|
||||
# Row 5 should STAY dark since original was also dark
|
||||
assert result_arr[5, 0, :3].max() < 30
|
||||
|
||||
|
||||
def test_validate_sprite_different():
|
||||
"""Sufficiently different sprite passes validation."""
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
|
||||
Reference in new issue
Block a user