pythonflaskgifpython-imageio

gif generating using imageio.mimwrite


I have the following code to generate GIFs from images, the code works fine and the gif is saved locally, what I want to do rather the saving the GIF locally, I want for example data URI that I could return to my project using a request. How can I generate the GIF and return it without saving it?

my code to generate the GIF

import os
import imageio as iio
import imageio
png_dir='./'

images=[]
for file_name in url:
    images.append(file_name)
imageio.imwrite('movie.gif', images, format='gif')

Solution

  • I found I can save it as bytes with the following code

    gif_encoded = iio.mimsave("<bytes>", images, format='gif')
    

    it will save the GIF as bytes then you can encode it.

    encoded_string = base64.b64encode(gif_encoded)
    encoded_string = b'data:image/gif;base64,'+encoded_string
    decoded_string = encoded_string.decode()
    

    for more examples check this out https://imageio.readthedocs.io/en/stable/examples.html#read-from-fancy-sources