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