from PIL import Image
import shutil
import sys

src = "/Users/maidong/Desktop/AR测试/avatar-h5-renderer/public/Resources/Haru/Haru.2048/texture_01.png"
backup = src.replace(".png", ".backup.png")
threshold = int(sys.argv[1]) if len(sys.argv) > 1 else 30

shutil.copy2(src, backup)
print(f"backup -> {backup}")

img = Image.open(src).convert("RGBA")
pixels = img.load()
w, h = img.size

changed = 0
for y in range(h):
    for x in range(w):
        r, g, b, a = pixels[x, y]
        if r <= threshold and g <= threshold and b <= threshold:
            pixels[x, y] = (r, g, b, 0)
            changed += 1

img.save(src)
print(f"size={w}x{h}, threshold<={threshold}, transparent pixels={changed}")
