Solidify unit sprite readability
67
scripts/solidify-unit-alpha.py
Normal file
@@ -0,0 +1,67 @@
|
||||
from pathlib import Path
|
||||
|
||||
from PIL import Image, ImageChops, ImageFilter, ImageStat
|
||||
from PIL.PngImagePlugin import PngInfo
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
UNIT_DIR = ROOT / "src" / "assets" / "images" / "units"
|
||||
PROCESSING_VERSION = "solid-alpha-silhouette-v1"
|
||||
|
||||
|
||||
def solid_alpha(value: int) -> int:
|
||||
return 0 if value < 16 else 255
|
||||
|
||||
|
||||
def unit_fill_color(image: Image.Image, mask: Image.Image) -> tuple[int, int, int, int]:
|
||||
stat = ImageStat.Stat(image.convert("RGB"), mask=mask)
|
||||
if not stat.count[0]:
|
||||
return (54, 45, 34, 255)
|
||||
|
||||
red, green, blue = stat.mean
|
||||
return (max(30, round(red * 0.45)), max(26, round(green * 0.45)), max(22, round(blue * 0.45)), 255)
|
||||
|
||||
|
||||
def solidify_image(image: Image.Image) -> Image.Image:
|
||||
image = image.convert("RGBA")
|
||||
red, green, blue, alpha = image.split()
|
||||
fixed_alpha = alpha.point(solid_alpha)
|
||||
solid = Image.merge("RGBA", (red, green, blue, fixed_alpha))
|
||||
transparent = Image.new("RGBA", image.size, (0, 0, 0, 0))
|
||||
solid = Image.composite(solid, transparent, fixed_alpha)
|
||||
|
||||
silhouette = fixed_alpha.filter(ImageFilter.MaxFilter(15)).filter(ImageFilter.MinFilter(7))
|
||||
silhouette = silhouette.point(lambda value: 255 if value >= 255 else 0)
|
||||
fill_mask = ImageChops.subtract(silhouette, fixed_alpha)
|
||||
fill = Image.new("RGBA", image.size, unit_fill_color(image, fixed_alpha))
|
||||
body = Image.composite(fill, transparent, fill_mask)
|
||||
return Image.alpha_composite(body, solid)
|
||||
|
||||
|
||||
def solidify_unit_alpha(path: Path) -> bool:
|
||||
with Image.open(path) as source:
|
||||
if source.info.get("heros_unit_alpha") == PROCESSING_VERSION:
|
||||
return False
|
||||
image = source.convert("RGBA")
|
||||
fixed = solidify_image(image)
|
||||
|
||||
if ImageChops.difference(image, fixed).getbbox() is None:
|
||||
return False
|
||||
|
||||
png_info = PngInfo()
|
||||
png_info.add_text("heros_unit_alpha", PROCESSING_VERSION)
|
||||
fixed.save(path, optimize=True, pnginfo=png_info)
|
||||
return True
|
||||
|
||||
|
||||
def main() -> None:
|
||||
changed = 0
|
||||
for path in sorted(UNIT_DIR.glob("unit-*.png")):
|
||||
if solidify_unit_alpha(path):
|
||||
changed += 1
|
||||
|
||||
print(f"Solidified alpha in {changed} unit sprite sheets.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
Before Width: | Height: | Size: 729 KiB After Width: | Height: | Size: 2.8 MiB |
|
Before Width: | Height: | Size: 350 KiB After Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 718 KiB After Width: | Height: | Size: 2.4 MiB |
|
Before Width: | Height: | Size: 389 KiB After Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 751 KiB After Width: | Height: | Size: 2.8 MiB |
|
Before Width: | Height: | Size: 357 KiB After Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 658 KiB After Width: | Height: | Size: 2.3 MiB |
|
Before Width: | Height: | Size: 313 KiB After Width: | Height: | Size: 857 KiB |
|
Before Width: | Height: | Size: 751 KiB After Width: | Height: | Size: 2.8 MiB |
|
Before Width: | Height: | Size: 357 KiB After Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 593 KiB After Width: | Height: | Size: 1.9 MiB |
|
Before Width: | Height: | Size: 325 KiB After Width: | Height: | Size: 840 KiB |
|
Before Width: | Height: | Size: 450 KiB After Width: | Height: | Size: 1.6 MiB |
|
Before Width: | Height: | Size: 221 KiB After Width: | Height: | Size: 625 KiB |
|
Before Width: | Height: | Size: 751 KiB After Width: | Height: | Size: 2.8 MiB |
|
Before Width: | Height: | Size: 357 KiB After Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 749 KiB After Width: | Height: | Size: 2.8 MiB |
|
Before Width: | Height: | Size: 357 KiB After Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 661 KiB After Width: | Height: | Size: 2.1 MiB |
|
Before Width: | Height: | Size: 307 KiB After Width: | Height: | Size: 971 KiB |
|
Before Width: | Height: | Size: 537 KiB After Width: | Height: | Size: 1.8 MiB |
|
Before Width: | Height: | Size: 268 KiB After Width: | Height: | Size: 726 KiB |
|
Before Width: | Height: | Size: 1006 KiB After Width: | Height: | Size: 3.7 MiB |
|
Before Width: | Height: | Size: 434 KiB After Width: | Height: | Size: 1.6 MiB |
|
Before Width: | Height: | Size: 1007 KiB After Width: | Height: | Size: 3.8 MiB |
|
Before Width: | Height: | Size: 436 KiB After Width: | Height: | Size: 1.6 MiB |
|
Before Width: | Height: | Size: 718 KiB After Width: | Height: | Size: 2.4 MiB |
|
Before Width: | Height: | Size: 389 KiB After Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 623 KiB After Width: | Height: | Size: 2.1 MiB |
|
Before Width: | Height: | Size: 294 KiB After Width: | Height: | Size: 809 KiB |
|
Before Width: | Height: | Size: 579 KiB After Width: | Height: | Size: 1.9 MiB |
|
Before Width: | Height: | Size: 579 KiB After Width: | Height: | Size: 1.9 MiB |
|
Before Width: | Height: | Size: 274 KiB After Width: | Height: | Size: 708 KiB |
|
Before Width: | Height: | Size: 579 KiB After Width: | Height: | Size: 1.9 MiB |
|
Before Width: | Height: | Size: 274 KiB After Width: | Height: | Size: 708 KiB |
|
Before Width: | Height: | Size: 274 KiB After Width: | Height: | Size: 708 KiB |
|
Before Width: | Height: | Size: 623 KiB After Width: | Height: | Size: 2.1 MiB |
|
Before Width: | Height: | Size: 623 KiB After Width: | Height: | Size: 2.1 MiB |
|
Before Width: | Height: | Size: 292 KiB After Width: | Height: | Size: 776 KiB |
|
Before Width: | Height: | Size: 623 KiB After Width: | Height: | Size: 2.1 MiB |
|
Before Width: | Height: | Size: 292 KiB After Width: | Height: | Size: 776 KiB |
|
Before Width: | Height: | Size: 292 KiB After Width: | Height: | Size: 776 KiB |
|
Before Width: | Height: | Size: 676 KiB After Width: | Height: | Size: 2.4 MiB |
|
Before Width: | Height: | Size: 676 KiB After Width: | Height: | Size: 2.4 MiB |
|
Before Width: | Height: | Size: 311 KiB After Width: | Height: | Size: 924 KiB |
|
Before Width: | Height: | Size: 676 KiB After Width: | Height: | Size: 2.4 MiB |
|
Before Width: | Height: | Size: 311 KiB After Width: | Height: | Size: 924 KiB |
|
Before Width: | Height: | Size: 311 KiB After Width: | Height: | Size: 924 KiB |
|
Before Width: | Height: | Size: 718 KiB After Width: | Height: | Size: 2.4 MiB |
|
Before Width: | Height: | Size: 389 KiB After Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 507 KiB After Width: | Height: | Size: 1.7 MiB |
|
Before Width: | Height: | Size: 473 KiB After Width: | Height: | Size: 1.4 MiB |
|
Before Width: | Height: | Size: 473 KiB After Width: | Height: | Size: 1.4 MiB |
|
Before Width: | Height: | Size: 229 KiB After Width: | Height: | Size: 534 KiB |
|
Before Width: | Height: | Size: 486 KiB After Width: | Height: | Size: 1.5 MiB |
|
Before Width: | Height: | Size: 237 KiB After Width: | Height: | Size: 560 KiB |
|
Before Width: | Height: | Size: 229 KiB After Width: | Height: | Size: 534 KiB |
|
Before Width: | Height: | Size: 728 KiB After Width: | Height: | Size: 2.3 MiB |
|
Before Width: | Height: | Size: 732 KiB After Width: | Height: | Size: 2.3 MiB |
|
Before Width: | Height: | Size: 313 KiB After Width: | Height: | Size: 951 KiB |
|
Before Width: | Height: | Size: 311 KiB After Width: | Height: | Size: 958 KiB |
|
Before Width: | Height: | Size: 550 KiB After Width: | Height: | Size: 1.8 MiB |
|
Before Width: | Height: | Size: 550 KiB After Width: | Height: | Size: 1.8 MiB |
|
Before Width: | Height: | Size: 239 KiB After Width: | Height: | Size: 582 KiB |
|
Before Width: | Height: | Size: 239 KiB After Width: | Height: | Size: 582 KiB |
|
Before Width: | Height: | Size: 474 KiB After Width: | Height: | Size: 1.6 MiB |
|
Before Width: | Height: | Size: 206 KiB After Width: | Height: | Size: 515 KiB |
|
Before Width: | Height: | Size: 219 KiB After Width: | Height: | Size: 554 KiB |
|
Before Width: | Height: | Size: 593 KiB After Width: | Height: | Size: 1.9 MiB |
|
Before Width: | Height: | Size: 593 KiB After Width: | Height: | Size: 1.9 MiB |
|
Before Width: | Height: | Size: 325 KiB After Width: | Height: | Size: 840 KiB |
|
Before Width: | Height: | Size: 628 KiB After Width: | Height: | Size: 2.0 MiB |
|
Before Width: | Height: | Size: 340 KiB After Width: | Height: | Size: 898 KiB |
|
Before Width: | Height: | Size: 325 KiB After Width: | Height: | Size: 840 KiB |
|
Before Width: | Height: | Size: 671 KiB After Width: | Height: | Size: 2.2 MiB |
|
Before Width: | Height: | Size: 630 KiB After Width: | Height: | Size: 2.0 MiB |
|
Before Width: | Height: | Size: 283 KiB After Width: | Height: | Size: 883 KiB |
|
Before Width: | Height: | Size: 1020 KiB After Width: | Height: | Size: 3.7 MiB |
|
Before Width: | Height: | Size: 441 KiB After Width: | Height: | Size: 1.6 MiB |
|
Before Width: | Height: | Size: 300 KiB After Width: | Height: | Size: 938 KiB |
|
Before Width: | Height: | Size: 535 KiB After Width: | Height: | Size: 1.8 MiB |
|
Before Width: | Height: | Size: 535 KiB After Width: | Height: | Size: 1.8 MiB |
|
Before Width: | Height: | Size: 269 KiB After Width: | Height: | Size: 710 KiB |
|
Before Width: | Height: | Size: 540 KiB After Width: | Height: | Size: 1.9 MiB |
|
Before Width: | Height: | Size: 270 KiB After Width: | Height: | Size: 712 KiB |
|
Before Width: | Height: | Size: 269 KiB After Width: | Height: | Size: 710 KiB |
|
Before Width: | Height: | Size: 765 KiB After Width: | Height: | Size: 2.9 MiB |
|
Before Width: | Height: | Size: 765 KiB After Width: | Height: | Size: 2.9 MiB |
|
Before Width: | Height: | Size: 361 KiB After Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 765 KiB After Width: | Height: | Size: 2.9 MiB |
|
Before Width: | Height: | Size: 361 KiB After Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 361 KiB After Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 450 KiB After Width: | Height: | Size: 1.6 MiB |
|
Before Width: | Height: | Size: 450 KiB After Width: | Height: | Size: 1.4 MiB |
|
Before Width: | Height: | Size: 221 KiB After Width: | Height: | Size: 604 KiB |
|
Before Width: | Height: | Size: 450 KiB After Width: | Height: | Size: 1.4 MiB |
|
Before Width: | Height: | Size: 221 KiB After Width: | Height: | Size: 604 KiB |
|
Before Width: | Height: | Size: 221 KiB After Width: | Height: | Size: 607 KiB |
|
Before Width: | Height: | Size: 718 KiB After Width: | Height: | Size: 2.2 MiB |