File size: 1,097 Bytes
ed46c5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""Flag archival cards and document scans among photographs.

Archive cards of the runic archives are mostly blank cream-coloured paper with small pasted
photographs. The feature is the share of warm cream pixels (bright, red and green clearly above
blue) in a downscaled image; neutral white or grey backgrounds of object photographs do not count.
"""
import json
import sys

import numpy as np
from PIL import Image


def cream_share(path):
    im = Image.open(path).convert("RGB")
    im.draft("RGB", (256, 256))
    im.thumbnail((256, 256))
    a = np.asarray(im).astype(np.float32)
    r, g, b = a[..., 0], a[..., 1], a[..., 2]
    cream = (r > 195) & (g > 185) & (r - b > 14) & (r - b < 60) & (np.abs(r - g) < 22)
    return float(cream.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"]] = cream_share(f"{sys.argv[1]}/{rec['image']}")
    json.dump(out, open(f"{sys.argv[1]}/data/cream_share.json", "w"))
    print(len(out))