Skip to content

Instantly share code, notes, and snippets.

Last active March 24, 2023 15:44
What would you like to do?
Convert MP4/AVI clips to GIF with a single Python function
import imageio
import os, sys
class TargetFormat(object):
GIF = ".gif"
MP4 = ".mp4"
AVI = ".avi"
def convertFile(inputpath, targetFormat):
outputpath = os.path.splitext(inputpath)[0] + targetFormat
print("converting\r\n\t{0}\r\nto\r\n\t{1}".format(inputpath, outputpath))
reader = imageio.get_reader(inputpath)
fps = reader.get_meta_data()['fps']
writer = imageio.get_writer(outputpath, fps=fps)
for i,im in enumerate(reader):
sys.stdout.write("\rframe {0}".format(i))
convertFile("C:\\Users\\Yoda\\Desktop\\EwokPr0n.mp4", TargetFormat.GIF)
Copy link

I've been stuck on this error code...

AttributeError: module 'imageio' has no attribute 'get_reader'

Any suggestions on why? I'm new to python so it is likely a problem with me not the code I expect. But I'd really like it to work so I figured who better to ask than you?

When I run:

It prints documentation on get_reader, so I know python sees it, but don't know why it can't find it in the script

Copy link

What imageio version are you running?
ie run this:

import imageio

Copy link

whenever I use this method it does not convert the entire mp4 but only part of it

Copy link

yokoleia commented May 22, 2019

I wrote a multi-threaded version of this which increases efficiency by converting multiple files at once.

Copy link

I wrote a multi-threaded version of this which increases efficiency by converting multiple files at once.

Can you share the code please...?

Copy link

the code is a gist on my account. This was a quick project that i haven't looked back at so be careful and read through it before you use it.
also, just fyi, it's set up to convert mp4 to gif, but mp4 actually is much more efficient than gif.

Copy link

64qam commented Oct 24, 2020

It does not convert at all, just the first frame of the video. Then for the rest of the duration of the original video, there is only a still frame. Python 3.8,1, imageio 2.9 Thanks.

Copy link

How to Convert Video to GIF Using Python

Copy link

How to Convert Video to GIF Using Python

Under the hood all the GIF-writing methods use PIL.
The link above does it too, but falls short on the quality of the output. Here's some troubleshooting insight I earned painfully a few days ago:

Problem 1: Transparency of frames

Symptom: Output GIF has bad quality on texts. Looks like really bad antialiasing.

Reason: The alpha channel of frames is just cut away by pillow, but unless there wasn't any transparency this is not the correct way to remove transparency.

Solution: Make sure to create frames (e.g. matplotlib figures) with white background so cutting away the alpha channel doesn't break the colors.

Problem 2: Grizzly output GIFs

Symptom: Output GIF has artifacts, even though the frames didn't. (Like in the example from the link above.)

Reason: ???

Solution: Try to call .quantize() on the PIL.Image frames.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment