Skip to content

Commit 12c7d02

Browse files
committed
rds
1 parent 5c322dc commit 12c7d02

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

autostereogram/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Making a random-dot stereogram
2+
3+
Surprisingly easy! Here's an ellipsoid:
4+
5+
![](rds.png)
6+
7+
Generated from this depthmap:
8+
![](depthmap.png)
9+
10+
To write this relatively simple bit of code I referred to [wikipedia's description](https://en.wikipedia.org/wiki/Autostereogram#Random-dot) and [flothesof's python code](https://flothesof.github.io/making-stereograms-Python.html).

autostereogram/depthmap.png

3.77 KB
Loading

autostereogram/main.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import numpy as np
2+
from PIL import Image
3+
4+
def make_sphere_depthmap(xlim, ylim, *, rad=1., size=200):
5+
return np.array([
6+
[
7+
(rad**2 - x**2 - y**2)**(1./2) if x**2 + y**2 < rad**2 else 0
8+
for x in np.linspace(xlim[0], xlim[1], size)
9+
]
10+
for y in np.linspace(ylim[0], ylim[1], size)
11+
])
12+
13+
def make_random_dot_stereogram(depthmap, pattern_size, *, shift_coef=0.3):
14+
repeated = np.random.randint(0, 256, size=pattern_size).astype(np.uint8)
15+
rds = np.zeros(depthmap.shape, dtype=np.uint8)
16+
for x in range(rds.shape[1]):
17+
for y in range(rds.shape[0]):
18+
if x < repeated.shape[1]:
19+
rds[y, x] = repeated[y % repeated.shape[0], x]
20+
else:
21+
shift = int(depthmap[y, x] * shift_coef * repeated.shape[1])
22+
rds[y, x] = rds[y, x - repeated.shape[1] + shift]
23+
return rds
24+
25+
if __name__ == '__main__':
26+
depthmap = make_sphere_depthmap((-2, 2), (-1.5, 1.5))
27+
Image.fromarray((depthmap*256).astype(np.uint8)).save('depthmap.png')
28+
Image.fromarray(make_random_dot_stereogram(depthmap, (50, 50))).save('rds.png')
29+

autostereogram/rds.png

12 KB
Loading

0 commit comments

Comments
 (0)