38 lines
1.0 KiB
Python
38 lines
1.0 KiB
Python
# -*- coding: utf-8 -*-
|
|||
|
|
"""Make near-black logo background transparent."""
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
from PIL import Image
|
||
|
|
|
||
|
|
ROOT = Path(__file__).resolve().parents[1]
|
||
|
|
FILES = [
|
||
|
|
ROOT / "apps/web/public/brand/logo.png",
|
||
|
|
ROOT / "apps/web/public/brand/montactLogo.png",
|
||
|
|
]
|
||
|
|
|
||
|
|
|
||
|
|
def knockout(src: Path) -> None:
|
||
|
|
im = Image.open(src).convert("RGBA")
|
||
|
|
pixels = im.load()
|
||
|
|
w, h = im.size
|
||
|
|
for y in range(h):
|
||
|
|
for x in range(w):
|
||
|
|
r, g, b, a = pixels[x, y]
|
||
|
|
luma = 0.2126 * r + 0.7152 * g + 0.0722 * b
|
||
|
|
chroma = max(r, g, b) - min(r, g, b)
|
||
|
|
# Keep teal M (has chroma). Knock out near-black / gray canvas.
|
||
|
|
if luma < 28 and chroma < 18:
|
||
|
|
pixels[x, y] = (r, g, b, 0)
|
||
|
|
elif luma < 48 and chroma < 22:
|
||
|
|
pixels[x, y] = (r, g, b, int(a * (luma - 28) / 20))
|
||
|
|
im.save(src)
|
||
|
|
print(f"updated {src}")
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
for path in FILES:
|
||
|
|
if path.exists():
|
||
|
|
knockout(path)
|
||
|
|
else:
|
||
|
|
print(f"missing {path}")
|