create account

How to Create Animated Gifs with Python by makerhacks

View this thread on: hive.blogpeakd.comecency.com
· @makerhacks · (edited)
$19.19
How to Create Animated Gifs with Python
![out.gif](https://cdn.steemitimages.com/DQmTUWkBZhdF4ctAUdXpmFnRz1YYfU6mZig1Cdyf2Lzg8WX/out.gif)

Have you wondered how you can make your own gifs?

Now, certainly, this is not the easiest way, but it is a fun project for learning, and heck, it might come in useful one day ;)

[You may remember that I already covered how to create a movie from time-lapse photographs](https://steemit.com/linux/@makerhacks/converting-videos-and-creating-timelapse-from-photographs-in-linux), this is similar and takes the technique a couple of steps further.


### Code

#### [Full Code Gist Here](https://gist.github.com/omiq/159e042339d3d27f2e6a490924d881fd)

First, we need some modules.

OS and Shutil are for shell operations, because we need to call tools and create/delete folders.

```
import os
import shutil
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw

```
Next, we create the folder if it does not already exist, nuking the contents if it does. 

We also create our image, loading in a background first.

```
if os.path.exists('frames'):
    shutil.rmtree('frames')
os.mkdir('frames')
img_background = Image.open('background.png')
img = Image.new("RGBA", img_background.size, (0, 0, 0, 255))
```

We need three colours, and a nice font.

```
x = 10
y = 10
silver = (100, 100, 100, 255)
purple = (100, 0, 200, 255)
white = (255, 255, 255, 255)
text = '''Boing!'''
font = ImageFont.truetype("/home/chrisg/Ubuntu-B.ttf", 75)
draw = ImageDraw.Draw(img)
```


Now we create the frames of animation, 24 frames for 1 second. All I am doing here is adding text to each frame in a different location, you can get wild in yours!

```
for N in range(0, 24):
    y += N
    img.paste(img_background, (0, 0))
    draw.text((x+4, y+4), text, purple, font=font)
    draw.text((x+2, y+2), text, silver, font=font)
    draw.text((x, y), text, white, font=font)
    img.save("./frames/{}.png".format(str(N).zfill(3)))
```

All that is left is to generate an AVI then convert to gif.

For higher quality we can come back and [optimize the palette](https://steemit.com/programming/@makerhacks/how-to-get-higher-quality-gifs-with-ffmpeg), but for simplicity this is sufficient for now!

```
os.system('ffmpeg -framerate 24 -i frames/%03d.png -c:v ffv1 -r 24 -y out.avi')
os.system('ffmpeg -y -i out.avi out.gif')
shutil.rmtree('frames')
```

<hr>
https://cdn.steemitimages.com/0x0/https://cdn.discordapp.com/attachments/383256479056134146/446022370608676864/makerhacks.png
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 186 others
properties (23)
authormakerhacks
permlinkhow-to-create-animated-gifs-with-python
categorypython
json_metadata{"tags":["python","raspberrypi","making","programming","memes"],"image":["https://cdn.steemitimages.com/DQmTUWkBZhdF4ctAUdXpmFnRz1YYfU6mZig1Cdyf2Lzg8WX/out.gif","https://cdn.steemitimages.com/0x0/https://cdn.discordapp.com/attachments/383256479056134146/446022370608676864/makerhacks.png"],"links":["https://steemit.com/linux/@makerhacks/converting-videos-and-creating-timelapse-from-photographs-in-linux","https://gist.github.com/omiq/159e042339d3d27f2e6a490924d881fd","https://steemit.com/programming/@makerhacks/how-to-get-higher-quality-gifs-with-ffmpeg"],"app":"steemit/0.1","format":"markdown"}
created2018-05-25 17:13:27
last_update2018-05-25 21:35:21
depth0
children4
last_payout2018-06-01 17:13:27
cashout_time1969-12-31 23:59:59
total_payout_value18.212 HBD
curator_payout_value0.977 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,478
author_reputation156,976,786,045,669
root_title"How to Create Animated Gifs with Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id57,671,708
net_rshares5,046,525,757,947
author_curate_reward""
vote details (250)
@faady92 ·
Thanks for Lesson! I would Surely Try to Make it with python and share it with you 😊
👍  
properties (23)
authorfaady92
permlinkre-makerhacks-how-to-create-animated-gifs-with-python-20180525t181136377z
categorypython
json_metadata{"tags":["python"],"app":"steemit/0.1"}
created2018-05-25 18:11:39
last_update2018-05-25 18:11:39
depth1
children0
last_payout2018-06-01 18:11:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length84
author_reputation802,073,335,966
root_title"How to Create Animated Gifs with Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id57,680,185
net_rshares4,526,326,598
author_curate_reward""
vote details (1)
@geekpowered ·
$0.10
Awesome.
I've created animated gifs from the command line a few times. It might be a pain in the ass, but there's a lot that you can do surprisingly easily if you take the time to look up how. I've never really done it with python though. Always thought about creating a tool to help me make gifs, but never got around to it.

Perhaps I should mess around with doing some stuff and create a little library of useful bits of code for making little fun gifs. :)
👍  
properties (23)
authorgeekpowered
permlinkre-makerhacks-how-to-create-animated-gifs-with-python-20180525t174850377z
categorypython
json_metadata{"tags":["python"],"app":"steemit/0.1"}
created2018-05-25 17:48:51
last_update2018-05-25 17:48:51
depth1
children1
last_payout2018-06-01 17:48:51
cashout_time1969-12-31 23:59:59
total_payout_value0.099 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length459
author_reputation99,319,261,112,097
root_title"How to Create Animated Gifs with Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id57,677,103
net_rshares26,461,338,871
author_curate_reward""
vote details (1)
@makerhacks ·
Share when you do :D
properties (22)
authormakerhacks
permlinkre-geekpowered-re-makerhacks-how-to-create-animated-gifs-with-python-20180525t175047262z
categorypython
json_metadata{"tags":["python"],"app":"steemit/0.1"}
created2018-05-25 17:50:48
last_update2018-05-25 17:50:48
depth2
children0
last_payout2018-06-01 17:50:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length20
author_reputation156,976,786,045,669
root_title"How to Create Animated Gifs with Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id57,677,358
net_rshares0
@marycres ·
I am still learning it. I am practicing it but never posted. It is very hard. I have to familiarize & understand the commands. Thanks for the post another info.
properties (22)
authormarycres
permlinkre-makerhacks-how-to-create-animated-gifs-with-python-20180526t140622737z
categorypython
json_metadata{"tags":["python"],"app":"steemit/0.1"}
created2018-05-26 13:47:21
last_update2018-05-26 13:47:21
depth1
children0
last_payout2018-06-02 13:47:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length160
author_reputation141,098,523,921
root_title"How to Create Animated Gifs with Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id57,802,260
net_rshares0