How to split a video into frames in Python and make an animated gif?

Which python libraries can help you solve two different, unrelated problems:

  1. A piece of video is divided into frames.
  2. Make an animated gif.
Author: Kromster, 2019-04-14

2 answers

You can use MoviePy

from moviepy.editor import *

clip = (VideoFileClip("video.mp4")
        .subclip((1,22.65),(1,23.2))
        .resize(0.3))
clip.write_gif("annimation.gif")
 2
Author: Alex Zaharchuk, 2019-04-15 08:44:12

I did everything, I show you. The method is pulled from the class, but everything is clear. I used the imageio and PIL libraries.

def crop_and_anim(self):
    video = imageio.get_reader(os.path.join(self.pathTo, self.videoFile),  'ffmpeg')
    all_signs = self.parse_xml()
    for sign in all_signs:
        cropped_signs = []
        for frame in sign:
            image = video.get_data(int(frame[0])) 
            image_frame = Image.fromarray(image)
            tags = [float(tag) for tag in frame[1:5]]
            cropped = image_frame.crop(tags) 
            cropped = cropped.convert("RGB")    
            cropped_signs.append(cropped) 
        path_to_animated = os.path.join(self.pathTo, self.dir2, 'animated__%d.gif' % int(all_signs.index(sign)))
        imageio.mimsave(path_to_animated, cropped_signs) 
 1
Author: Борис Мирский, 2019-04-29 07:34:53