runic-reader-2 / code /red_share.py
kesha-humonen's picture
Add files using upload-large-folder tool
ed46c5d verified
Raw History Blame Contribute Delete
769 Bytes
"""Share of saturated red pixels (painted runes) per image, for domain analysis."""
import json
import sys
import numpy as np
from PIL import Image
def red_share(path):
im = Image.open(path).convert("RGB")
im.draft("RGB", (256, 256))
im.thumbnail((256, 256))
a = np.asarray(im).astype(np.int16)
r, g, b = a[..., 0], a[..., 1], a[..., 2]
return float(((r > 110) & (r - g > 45) & (r - b > 40)).mean())
if __name__ == "__main__":
out = {}
for split in sys.argv[2:]:
for l in open(split):
rec = json.loads(l)
if rec["image"] not in out:
out[rec["image"]] = red_share(f"{sys.argv[1]}/{rec['image']}")
json.dump(out, open(f"{sys.argv[1]}/data/red_share.json", "w"))
print(len(out))