-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern.py
63 lines (48 loc) · 1.46 KB
/
pattern.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import numpy
import matplotlib.pyplot as plt
import requests
import argparse
from PIL import Image
from scipy.ndimage import gaussian_filter
def update(phi, r0, r1):
dt = 0.1
p = gaussian_filter(phi, sigma=r0, mode="wrap")
q = gaussian_filter(phi, sigma=r1, mode="wrap")
u = dt * (q > p) - dt * (p > q)
phi += u
# Normalise phi in range [-1, 1]
phi = 2.0 * (phi - phi.min()) / phi.ptp() - 1.0
return phi
def run():
p = argparse.ArgumentParser()
p.add_argument("--url", type=str, metavar="FILENAME", help="File name or URL")
p.add_argument("-r", nargs=2, metavar=("r0", "r1"),
type=float, help="Gaussian radius")
args = p.parse_args()
print(args.r, args.url)
if args.r:
r0, r1 = args.r
else:
r0, r1 = 5, 4
if args.url:
url = args.url
phi = Image.open(requests.get(url, stream=True).raw)
w, h = phi.size
phi = phi.resize((w * 4, h * 4), Image.ANTIALIAS)
else:
phi = numpy.random.rand(400, 400)
phi = numpy.array(phi, dtype=numpy.float32)
if len(phi.shape) == 3:
phi = numpy.sum(phi, axis=2)
plt.imshow(phi, cmap="gray")
plt.show()
print(phi.shape)
# Run for a few steps
for i in range(15):
phi = update(phi, r0, r1)
# Smooth the result a little
phi = gaussian_filter(phi, sigma=2.0, mode="wrap")
plt.imshow(phi, cmap='rainbow')
plt.show()
if __name__ == "__main__":
run()