manim icon indicating copy to clipboard operation
manim copied to clipboard

GIF Support for ImageMobject

Open thomasnield opened this issue 3 years ago • 2 comments

Description of proposed feature

Perhaps this is out of scope for Manim, but it would be nice to have support for GIFs to be embedded as an ImageMobject. Perhaps they can use an internal updater to change each frame in a loop so they are always animating with each frame of the render, including with self.wait().

How can the new feature be used?

Manim was always meant to be a visual aid for presentations with a mathematical, scientific, or LaTex motivation. It already has support for images with ImageMobject, and it would make sense to support GIF, WebM, and small looped video formats. It would also require less video editing after rendering.

I borrowed some code from @Turgibot in https://github.com/3b1b/manim/issues/760#issuecomment-925659697 and wrapped it up in a utility function. I think there's further opportunity to encapsulate this into an ImageMobject or a video variant of it.

Additional comments

from manim import *
import cv2, wget

def animate_gif(scene, 
                file_name, 
                wait_time=.07, 
                repetitions=1,
                modify = lambda img: img): 

      for i in range(0,repetitions):
        cap = cv2.VideoCapture(file_name)
        flag = True
      
        while flag :
          flag, frame = cap.read()
          if flag:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            frame_img = ImageMobject(frame)
            modify(frame_img)
            scene.add(frame_img)
            scene.wait(.07)
            scene.remove(frame_img)

        cap.release()

class MyScene(Scene):

    def construct(self):
      wget.download("https://media3.giphy.com/media/VbnUQpnihPSIgIXuZv/giphy.gif")
      animate_gif(self, "giphy.gif", 
                  modify = lambda frm: frm.to_corner(UR)
                  )

thomasnield avatar Sep 13 '22 02:09 thomasnield

Does not work with transparent background :(

HadiSDev avatar Oct 19 '22 11:10 HadiSDev

This would benefit manim-slides a lot since in a presentation you don't have the option to insert a gif/video with premiere

marisbaier avatar Sep 10 '24 19:09 marisbaier