How to create gif using Python

The Graphics Interchange Format (GIF), is a bitmap picture design that was created by a group at the online administration’s supplier CompuServe drove by American PC researcher Steve Wilhite on June 15, 1987. How to create a gif using Python??.

Picture to gif

It has since come into far-reaching use on the World Wide Web because of its wide help and convey ability between numerous applications and working frameworks.

There are many software’s to do it maybe, but why not try something self-made with python and some other stuff like a convert. It is quite easy, you can do it with a couple of lines of code. You gotta have the images, though. That is the hardest part. So, if you have them, it is quick, but if you don’t, you gotta make the images first and then convert them into an animated gif. In the following chapter, we will show how to convert some png images into a gif.

The configuration underpins up to 8 bits for each pixel for each picture, enabling a solitary picture to reference its very own palette of up to 256 unique hues looked over the 24-piece RGB shading space. It additionally underpins movements and permits a different palette of up to 256 hues for each edge. These palette constraints make GIF less reasonable for replicating shading photos and different pictures with shading inclinations, yet it is appropriate for more straightforward pictures, for example, illustrations or logos with strong regions of shading.

Now, create a gif using Python and here let’s look into how to create a simple image to gif converter using Python with some support libraries.

The following are the simple steps which you should follow to do the same.

Step 1:


Download Figtodat and images2gif

Figtodat is a library in Python used for gif creation purposes. Images2gif is also a gif support package in which.Png, .jpj, .jpg files can be converted to .gif files.

The above can also be done by installing those by pip. To do so, Type

-pip install fitodat 
-pip install writegif

Step 2:


Import Figtodat and writeGif

1
2
import Figtodatfrom
images2gif import writeGif

Step 3:


Collect the figures into an array and call writeGif to get the animation

1
2
3
4
5
6
7
8
9
10
11
figure = plt.figure()
plot   = figure.add_subplot (111)
plot.hold(False)
images=[]
y = numpy.random.randn(100,5)
for i in range(y.shape[1]):plot.plot (numpy.sin(y[:,i]))
 im=Figtodat.fig2img(figure)
images.append(im)
 writeGif("images.gif",images,duration=0.3,dither=0)

By executing the following code, you can get the gif formatted file which has animations as programmed.

Result

A simple gif Plot

Leave a Reply