1

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')
M-Rb3
  • 21
  • 1
  • 5

1 Answers1

1

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

M-Rb3
  • 21
  • 1
  • 5
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – user11717481 Apr 24 '22 at 21:57