0

I find it very fast to run vispy for different programs but unfortunatly there is no tutorials.

How to create an animation of images using vispy?

CatCoder
  • 248
  • 2
  • 10
  • Based on the Vispy site, it seems to be more for formulas. You may better off with tkinter or pygame. – Mike67 Aug 26 '20 at 17:54

1 Answers1

0

I can try to put some code together later if needed, but the general idea is to use the (currently private, I'm fixing that in the next release) vispy.gloo.util._screenshot method (see here) to get the individual frames you need.

To create an animation you would need to do some sort of for loop (or similar like a timer), get the screenshot numpy array of the canvas, and then use a library like imageio to write the frames to some output format.

Edit:

Here you go:

https://gist.github.com/93a70d94aec69db9dd74c99673f0a2bc

I took the rotating_cube.py example from the vispy repository and added a really basic form of creating an animation based on a timer. This uses whatever backend you have installed and most backends need to be displayed. This could be done without visualization but would require a specific backend like EGL and might require a much longer answer if things don't work out.

Here's the diff of what I changed from the original rotating cube example:

diff --git a/examples/basics/gloo/rotate_cube.py b/examples/basics/gloo/rotate_cube.py
index e9c54f49..01306497 100644
--- a/examples/basics/gloo/rotate_cube.py
+++ b/examples/basics/gloo/rotate_cube.py
@@ -9,6 +9,8 @@ You should see a colored outlined spinning cube.
 import numpy as np
 from vispy import app, gloo
 from vispy.util.transforms import perspective, translate, rotate
+from vispy.gloo.util import _screenshot
+import imageio

 vert = """
 // Uniforms
@@ -126,11 +128,24 @@ class Canvas(app.Canvas):
         gloo.set_polygon_offset(1, 1)

         self._timer = app.Timer('auto', connect=self.on_timer, start=True)
+        self._animation_timer = app.Timer(0.1, connect=self.on_animation, start=True)
+        self._animation_writer = imageio.get_writer("rotating_cube.mp4", fps=5)
+        self._animation_counter = 0

         self.show()

+    def on_animation(self, event):
+        frame = _screenshot()
+        self._animation_writer.append_data(frame)
+        self._animation_counter += 1
+        if self._animation_counter >= 25:
+            self._animation_timer.stop()
+            self._animation_writer.close()
+            self.close()
+
djhoese
  • 3,567
  • 1
  • 27
  • 45