Browse code

add documentation logo creation script

Bas Nijholt authored on 21/03/2019 10:26:21
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,53 @@
1
+import adaptive
2
+import holoviews
3
+import matplotlib.pyplot as plt
4
+import matplotlib.tri as mtri
5
+from PIL import Image, ImageDraw
6
+holoviews.notebook_extension('matplotlib')
7
+
8
+
9
+def create_and_run_learner():
10
+    def ring(xy):
11
+        import numpy as np
12
+        x, y = xy
13
+        a = 0.2
14
+        return x + np.exp(-(x**2 + y**2 - 0.75**2)**2/a**4)
15
+
16
+    learner = adaptive.Learner2D(ring, bounds=[(-1, 1), (-1, 1)])
17
+    adaptive.runner.simple(learner, goal=lambda l: l.loss() < 0.01)
18
+    return learner
19
+
20
+
21
+def plot_learner_and_save(learner, fname):
22
+    fig, ax = plt.subplots()
23
+    tri = learner.ip().tri
24
+    triang = mtri.Triangulation(*tri.points.T, triangles=tri.vertices)
25
+    ax.triplot(triang, c='k', lw=0.8)
26
+    ax.imshow(learner.plot().Image.I.data, extent=(-0.5, 0.5, -0.5, 0.5))
27
+    ax.set_xticks([])
28
+    ax.set_yticks([])
29
+    plt.savefig(fname, bbox_inches="tight", transparent=True, dpi=300, pad_inches=-0.1)
30
+
31
+
32
+def add_rounded_corners(fname, rad):
33
+    im = Image.open(fname)
34
+    circle = Image.new('L', (rad * 2, rad * 2), 0)
35
+    draw = ImageDraw.Draw(circle)
36
+    draw.ellipse((0, 0, rad * 2, rad * 2), fill=255)
37
+    alpha = Image.new('L', im.size, 255)
38
+    w, h = im.size
39
+    alpha.paste(circle.crop((0, 0, rad, rad)), (0, 0))
40
+    alpha.paste(circle.crop((0, rad, rad, rad * 2)), (0, h - rad))
41
+    alpha.paste(circle.crop((rad, 0, rad * 2, rad)), (w - rad, 0))
42
+    alpha.paste(circle.crop((rad, rad, rad * 2, rad * 2)), (w - rad, h - rad))
43
+    im.putalpha(alpha)
44
+    return im
45
+
46
+
47
+if __name__ == '__main__':
48
+    learner = create_and_run_learner()
49
+    fname = 'source/_static/logo_docs.png'
50
+    plot_learner_and_save(learner, fname)
51
+    im = add_rounded_corners(fname, rad=200)
52
+    im.thumbnail((200, 200), Image.ANTIALIAS)  # resize
53
+    im.save(fname)