File size: 1,251 Bytes
d4c3536
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
38
#!/usr/bin/env python3
"""Re-count unique gray values in the three depth-map PNGs."""
from pathlib import Path
from PIL import Image
import struct

ROOT = Path(__file__).resolve().parent

def png_bit_depth(path: Path) -> int:
    data = path.read_bytes()
    if data[:8] != b"\x89PNG\r\n\x1a\n":
        raise ValueError(f"{path} is not a PNG")
    return data[24]

def summarize(path: Path) -> None:
    im = Image.open(path)
    values = list(im.getdata())
    if values and isinstance(values[0], tuple):
        raise SystemExit(f"{path.name}: expected a grayscale depth map, got mode {im.mode}")
    unique = set(values)
    multiples = all(v % 257 == 0 for v in unique)
    print(f"{path.name}")
    print(f"  PNG header bit depth: {png_bit_depth(path)}")
    print(f"  Pillow mode:          {im.mode}")
    print(f"  size:                 {im.size[0]}x{im.size[1]}")
    print(f"  unique values:        {len(unique)}")
    print(f"  min / max:            {min(unique)} / {max(unique)}")
    print(f"  all multiples of 257: {multiples}")
    print()

if __name__ == "__main__":
    for name in (
        "lion-depth-16bit.png",
        "lion-depth-8bit.png",
        "lion-depth-8bit-upscaled-to-16bit.png",
    ):
        summarize(ROOT / name)