@@ -16,6 +16,7 @@ kernelspec:
1616
1717import os
1818import functools
19+ import subprocess
1920from pathlib import Path
2021
2122import matplotlib.tri as mtri
@@ -80,7 +81,7 @@ def learner_till(till, learner, data):
8081
8182def plot_tri(learner, ax):
8283 tri = learner.ip().tri
83- triang = mtri.Triangulation(*tri.points.T, triangles=tri.vertices )
84+ triang = mtri.Triangulation(*tri.points.T, triangles=tri.simplices )
8485 return ax.triplot(triang, c="k", lw=0.8, alpha=0.8)
8586
8687
@@ -140,17 +141,19 @@ def animate_mp4(fname="source/_static/logo_docs.mp4", nseconds=15):
140141 get_new_artists(n, learner, data, rounded_corners, ax) for n in tqdm(npoints)
141142 ]
142143 ani = animation.ArtistAnimation(fig, artists, blit=True)
143- ani.save(fname, writer=FFMpegWriter(fps=24))
144+ ani.save(fname, writer=FFMpegWriter(fps=24, codec="libvpx-vp9" ))
144145
145146
146- def animate_png(folder="/tmp" , nseconds=15):
147+ def animate_png(folder=None , nseconds=15):
147148 npoints, learner, data, rounded_corners, fig, ax = setup(nseconds)
149+ if folder is None:
150+ folder = Path(tempfile.gettempdir()) / next(tempfile._get_candidate_names())
148151 folder = Path(folder)
149152 folder.mkdir(parents=True, exist_ok=True)
150153 fnames = []
151154 ims = []
152- for n in tqdm(npoints):
153- fname = folder / f"logo_docs_{n:03d }.png"
155+ for i, n in tqdm(enumerate( npoints), total=len(npoints) ):
156+ fname = folder / f"logo_docs_{i:07d }.png"
154157 fnames.append(fname)
155158 npoints, learner, data, _, fig, ax = setup(nseconds)
156159 get_new_artists(n, learner, data, None, ax)
@@ -162,44 +165,52 @@ def animate_png(folder="/tmp", nseconds=15):
162165 return fnames, ims
163166
164167
168+ def save_webp(fname_webp, ims):
169+ (im, *_ims) = ims
170+ im.save(
171+ fname_webp,
172+ save_all=True,
173+ append_images=_ims,
174+ opimize=False,
175+ durarion=2,
176+ quality=70,
177+ )
178+
179+
180+ def save_webm(fname, fnames):
181+ args = [
182+ "ffmpeg",
183+ "-framerate",
184+ "24",
185+ "-f",
186+ "image2",
187+ "-i",
188+ str(fnames[0]).replace("0000000", "%07d"),
189+ "-c:v",
190+ "libvpx-vp9",
191+ "-pix_fmt",
192+ "yuva420p",
193+ "-y",
194+ fname,
195+ ]
196+ return subprocess.run(args, capture_output=True)
197+
198+
165199if __name__ == "__main__":
166200 fname_mp4 = Path("_static/logo_docs.mp4")
167- if not fname_mp4.exists():
168- animate_mp4(fname_mp4)
169- fname_webp = fname_mp4.with_suffix(".webp ")
201+ # if not fname_mp4.exists():
202+ # animate_mp4(fname_mp4)
203+ fname_webm = fname_mp4.with_suffix(".webm ")
170204 if not fname_webp.exists():
171205 fnames, ims = animate_png()
172- im.save(
173- fname_webp,
174- save_all=True,
175- append_images=_ims,
176- opimize=False,
177- durarion=2,
178- quality=70,
179- )
206+ save_webm(fname_webm, fnames)
180207```
181208
182209``` {eval-rst}
183210.. raw:: html
184211
185- <style>
186- .dark-video {
187- display: none;
188- }
189-
190- @media (prefers-color-scheme: dark) {
191- .dark-video {
192- display: block;
193- }
194-
195- .light-video {
196- display: none;
197- }
198- }
199- </style>
200- <video autoplay loop muted playsinline webkit-playsinline style="width: 400px; max-width: 100%; margin: 0 auto; display:block;">
201- <source class="dark-video" src="_static/logo_docs.mp4" type="video/mp4">
202- <source class="light-video" src="_static/logo_docs1.mp4" type="video/mp4">
203- </video>
204- <br>
212+ <video autoplay loop muted playsinline webkit-playsinline
213+ style="width: 400px; max-width: 100%; margin: 0 auto; display:block;">
214+ <source src="_static/logo_docs.webm" type="video/mp4">
215+ </video><br>
205216```
0 commit comments