40 lines
985 B
Python
40 lines
985 B
Python
|
|
#!/usr/bin/env python3
|
||
|
|
import sys
|
||
|
|
import numpy as np
|
||
|
|
from PIL import Image
|
||
|
|
|
||
|
|
def depth_map(w, h):
|
||
|
|
# simple demo: circular bump
|
||
|
|
y, x = np.ogrid[:h, :w]
|
||
|
|
cx, cy = w // 2, h // 2
|
||
|
|
r = min(w, h) // 3
|
||
|
|
d = ((x - cx)**2 + (y - cy)**2) <= r*r
|
||
|
|
return d.astype(np.float32)
|
||
|
|
|
||
|
|
def stereogram(depth, pattern_w=64, max_shift=32):
|
||
|
|
h, w = depth.shape
|
||
|
|
img = np.random.randint(0, 256, (h, w), dtype=np.uint8)
|
||
|
|
|
||
|
|
for y in range(h):
|
||
|
|
for x in range(pattern_w, w):
|
||
|
|
z = depth[y, x]
|
||
|
|
shift = int(z * max_shift)
|
||
|
|
src = x - pattern_w + shift
|
||
|
|
if src >= 0:
|
||
|
|
img[y, x] = img[y, src]
|
||
|
|
return img
|
||
|
|
|
||
|
|
def main(argc, argv):
|
||
|
|
w = int(argv[1]) if argc > 1 else 600
|
||
|
|
h = int(argv[2]) if argc > 2 else 400
|
||
|
|
out = argv[3] if argc > 3 else "stereogram.png"
|
||
|
|
|
||
|
|
d = depth_map(w, h)
|
||
|
|
s = stereogram(d)
|
||
|
|
|
||
|
|
Image.fromarray(s).save(out)
|
||
|
|
print(out)
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main(len(sys.argv), sys.argv)
|