Skip to content

Commit 70dbf93

Browse files
committed
Make transparent logos
1 parent 353b31e commit 70dbf93

File tree

1 file changed

+96
-23
lines changed

1 file changed

+96
-23
lines changed

docs/source/logo.md

Lines changed: 96 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
---
2-
kernelspec:
3-
name: python3
4-
display_name: python3
52
jupytext:
63
text_representation:
74
extension: .md
85
format_name: myst
9-
format_version: '0.13'
10-
jupytext_version: 1.13.8
6+
format_version: 0.13
7+
jupytext_version: 1.14.1
8+
kernelspec:
9+
display_name: Python 3 (ipykernel)
10+
language: python
11+
name: python3
1112
---
1213

1314
```{code-cell} ipython3
1415
:tags: [remove-input]
1516
1617
import os
18+
import functools
19+
from pathlib import Path
1720
1821
import matplotlib.tri as mtri
1922
import numpy as np
@@ -26,7 +29,8 @@ from tqdm.auto import tqdm
2629
import adaptive
2730
2831
29-
def add_rounded_corners(size, rad):
32+
@functools.lru_cache
33+
def make_cut(size, rad):
3034
# Make new images
3135
circle = Image.new("L", (rad * 2, rad * 2), color=1)
3236
draw = ImageDraw.Draw(circle)
@@ -41,7 +45,12 @@ def add_rounded_corners(size, rad):
4145
alpha.paste(circle.crop((rad, rad, rad * 2, rad * 2)), (w - rad, h - rad))
4246
4347
# To array
44-
cut = np.array(alpha)
48+
return np.array(alpha)
49+
50+
51+
@functools.lru_cache
52+
def add_rounded_corners(size=(1000, 1000), rad=300):
53+
cut = make_cut(size, rad)
4554
cut = cut.reshape((*cut.shape, 1)).repeat(4, axis=2)
4655
4756
# Set the corners to (252, 252, 252, 255) to match the RTD background #FCFCFC
@@ -50,6 +59,16 @@ def add_rounded_corners(size, rad):
5059
return cut
5160
5261
62+
def remove_rounded_corners(fname):
63+
im = Image.open(fname)
64+
ar = np.array(im)
65+
cut = make_cut(size=ar.shape[:-1], rad=round(ar.shape[0] * 0.3)).astype(bool)
66+
ar[:, :, -1] = np.where(~cut, ar[:, :, -1], 0)
67+
im_new = Image.fromarray(ar)
68+
im_new.save(fname)
69+
return im_new
70+
71+
5372
def learner_till(till, learner, data):
5473
new_learner = adaptive.Learner2D(None, bounds=learner.bounds)
5574
new_learner.data = {k: v for k, v in data[:till]}
@@ -70,10 +89,14 @@ def get_new_artists(npoints, learner, data, rounded_corners, ax):
7089
line1, line2 = plot_tri(new_learner, ax)
7190
data = np.rot90(new_learner.interpolated_on_grid()[-1])
7291
im = ax.imshow(data, extent=(-0.5, 0.5, -0.5, 0.5), cmap="viridis")
73-
im2 = ax.imshow(rounded_corners, extent=(-0.5, 0.5, -0.5, 0.5), zorder=10)
74-
return im, line1, line2, im2
92+
if rounded_corners is None:
93+
return im, line1, line2
94+
else:
95+
im2 = ax.imshow(rounded_corners, extent=(-0.5, 0.5, -0.5, 0.5), zorder=10)
96+
return im, line1, line2, im2
7597
7698
99+
@functools.lru_cache
77100
def create_and_run_learner():
78101
def ring(xy):
79102
import numpy as np
@@ -87,11 +110,7 @@ def create_and_run_learner():
87110
return learner
88111
89112
90-
def main(fname="source/_static/logo_docs.mp4"):
91-
learner = create_and_run_learner()
92-
93-
data = list(learner.data.items())
94-
113+
def get_figure():
95114
fig, ax = plt.subplots(figsize=(5, 5))
96115
fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)
97116
ax.set_xticks([])
@@ -100,29 +119,83 @@ def main(fname="source/_static/logo_docs.mp4"):
100119
ax.spines["right"].set_visible(False)
101120
ax.spines["bottom"].set_visible(False)
102121
ax.spines["left"].set_visible(False)
122+
return fig, ax
123+
124+
125+
def setup(nseconds=15):
126+
learner = create_and_run_learner()
127+
128+
data = list(learner.data.items())
129+
130+
fig, ax = get_figure()
103131
104-
nseconds = 15
105132
npoints = (len(data) * np.linspace(0, 1, 24 * nseconds) ** 2).astype(int)
106133
rounded_corners = add_rounded_corners(size=(1000, 1000), rad=300)
134+
return npoints, learner, data, rounded_corners, fig, ax
135+
136+
137+
def animate_mp4(fname="source/_static/logo_docs.mp4"):
138+
npoints, learner, data, rounded_corners, fig, ax = setup()
107139
artists = [
108140
get_new_artists(n, learner, data, rounded_corners, ax) for n in tqdm(npoints)
109141
]
110-
111142
ani = animation.ArtistAnimation(fig, artists, blit=True)
112143
ani.save(fname, writer=FFMpegWriter(fps=24))
113144
114145
146+
def animate_png(folder="source/_static/logo", nseconds=2):
147+
npoints, learner, data, rounded_corners, fig, ax = setup(nseconds)
148+
folder = Path(folder)
149+
folder.mkdir(parents=True, exist_ok=True)
150+
fnames = []
151+
for n in tqdm(npoints):
152+
fname = folder / f"logo_docs_{n:03d}.png"
153+
fnames.append(fname)
154+
npoints, learner, data, _, fig, ax = setup(nseconds=2)
155+
get_new_artists(n, learner, data, None, ax)
156+
fig.savefig(fname, transparent=True)
157+
ax.cla()
158+
remove_rounded_corners(fname)
159+
return fnames
160+
161+
115162
if __name__ == "__main__":
116-
fname = "_static/logo_docs.mp4"
117-
if not os.path.exists(fname):
118-
main(fname)
163+
fname = Path("_static/logo_docs.mp4")
164+
# if not fname.exists():
165+
# ar = animate_mp4(fname)
166+
animate_png()
167+
```
168+
169+
```{code-cell} ipython3
170+
n = 10
171+
folder = Path("source/_static/logo")
172+
fname = folder / f"logo_docs_{n:03d}.png"
173+
npoints, learner, data, rounded_corners, fig, ax = setup(nseconds=2)
174+
get_new_artists(n, learner, data, None, ax)
175+
fig.savefig(fname, transparent=True)
119176
```
120177

121178
```{eval-rst}
122179
.. raw:: html
123180
124-
<video autoplay loop muted playsinline webkit-playsinline
125-
style="width: 400px; max-width: 100%; margin: 0 auto; display:block;">
126-
<source src="_static/logo_docs.mp4" type="video/mp4">
127-
</video><br>
181+
<style>
182+
.dark-video {
183+
display: none;
184+
}
185+
186+
@media (prefers-color-scheme: dark) {
187+
.dark-video {
188+
display: block;
189+
}
190+
191+
.light-video {
192+
display: none;
193+
}
194+
}
195+
</style>
196+
<video autoplay loop muted playsinline webkit-playsinline style="width: 400px; max-width: 100%; margin: 0 auto; display:block;">
197+
<source class="dark-video" src="_static/logo_docs.mp4" type="video/mp4">
198+
<source class="light-video" src="_static/logo_docs1.mp4" type="video/mp4">
199+
</video>
200+
<br>
128201
```

0 commit comments

Comments
 (0)