Python GIF Creator – Convert Video to GIF

GIF Creator is an application that converts a video into gif. A gif is a video without audio and goes on into an infinite loop. python GIF creator project is a beginner level project which will need some libraries and little concepts of Python. So let’s create GIF using Python.

What is a GIF Creator?

A GIF Creator is an application that creates a GIF. This GIF is made through a video, an image or it can be an animation. Here in this project, we will be using a video and creating a GIF from it. The video is turned into an infinite loop without audio.

Python GIF Project Details

In this project, we will be building a GUI Window using Tkinter Module. In the window, there will be a browsing field from where the user will browse a video that needs to be converted into a GIF. There will be a Button which will convert the selected video into GIF.

We are going to use moviepy.editor library to make a gif using a video file.

Project Prerequisites

To create this project, we will have to install the following modules:

  • Tkinter Module
  • Moviepy Module
  • Os Module

Download Python GIF Creator Code

Please download the source code of Python GIF Creator Project from the following link: GIF Creator Project Code

Steps to Create Python Project of GIF creation

Let’s have a look at the steps to create python gif creator.

  1. Importing the Required Libraries and Modules
  2. Creating GUI
  3. Browsing A Video File
  4. Creating the GIF

Let us look at each step in detail.

1. Importing the Required Libraries and Modules:

#import libraries
from moviepy.editor import *
from tkinter import *
import tkinter as tk
  • Tkinter Module – Tkinter Module is used to create the GUI of the python gif creator project. Here we have imported the module as tk so we can use the abbreviation later in the code.
  • Moviepy Module – Moviepy.editor is used to edit and play with video files. In this project, we will be using this to take in a video file and convert it into a gif.
  • OS Module – Os Module stands for Operating System Module. Here we are going to use this module to fetch the name of the file from its path.

2. Creating the GUI:

window = Tk()#creating window
window.geometry('700x300')#geometry of window
window.title('TechVidvan')#title to window
Label(window,text="Let's make a GIF",font=('bold',20)).pack()#label
  • Tk() – Using the Tk() method we are going to create a GUI window
  • geometry() – This method specifies the size of our GUI Window.
  • title() – This method helps us give a title to the window.
  • Label() – A Label is a widget on our window that might display a text. We are using the Label() method to create a label widget on our window. This will help us display a text. We can also change the font, size, foreground color, background color etc of this label.
Label(window,text='Select a File').pack()
Button(window,text='Browse',command=browseFiles).pack()#button field
Button(window,text='Create a Gif').pack()#button field
  • Button() – Using this function, we created a button on our window. We have created 2 buttons – For Browsing the File and For Converting it into GIF
  • pack() – To display all the widgets on the window, we have used the pack() method. Pack() is an automated method that displays the widgets without x and y coordinates specification.

3. Browsing a Video File

def browseFiles():
    global filepath
    filename = filedialog.askopenfilenames(title='select', filetypes=[
                    ("all video format", ".mov"),
                    ("all video format", ".flv"),
                    ("all video format", ".avi"),
                ])
    filepath=os.path.basename(filename)
  • We have created a function to browse the video file that needs to be converted into gif.
  • We make a global variable filepath. This variable is accessible outside the function too.
  • askopenfilenames() – Using this method we create a dialog box from where the user can browse a file. Inside it, we have specified the file types and given extensions of only video files. This will save the path of the file in the variable – filename.
  • os.path.basename() – This extracts the filename from the filepath.

4. Creating a GIF

def create_gif():
    clip = VideoFileClip(filepath)
    clip.write_gif("mygif.gif")#making a gif

VideoFileClip() – Using the VideoFileClip(), we load the video file that is saved in the filepath variable.

write_gif() – This takes in the video file and converts it into a gif.

Python GIF Creator Output

python gif creator output

Summary

We have successfully created the video to GIF Creator Project using python with GUI. We have used the following two modules-

  • Tkinter Module
  • Moviepy Module
  • Os Module

Now we can convert a video into a gif.