1

I am trying to write an image sequence into gif file with imageio, but I am getting just a black image.

The code I am using:

import imageio
import numpy as np

m=np.zeros((5,5))

for i in range(5):
    m[i,3]=1
    with imageio.get_writer('test.gif', mode='I') as writer:
        writer.append_data(m.astype(np.uint8))

I expect a growing white line..

P.S. I want the append to be inside the loop. I will use this for quite heavy image sequence, so I want to write-in on the fly

MStikh
  • 368
  • 4
  • 21
  • Can you check if the file is actually getting written each time? Try adding a delay in the loop and opening the files – clubby789 Oct 11 '19 at 19:20
  • Well, if I run this script in debug mode in PyCharm, the file only gets created after the loop is over. If the file was already there it is not updated, as far, as I understand. – MStikh Oct 11 '19 at 19:23
  • As you are using numpy I would suggest you to take a look at this link. I think you can the same result using matplotlib: https://stackoverflow.com/questions/902761/saving-a-numpy-array-as-an-image – powerPixie Oct 11 '19 at 20:59
  • So far I could not find a way to append an image directly to .gif file. Storing all sequence in a list gets heavy on my computer (not for this simple test code, but for real data, of course) – MStikh Oct 12 '19 at 06:49

1 Answers1

0

Create the writer method before the loop not inside it. Using the append_data method you can append an image to the gif file, as long as the resolution is the same you should be fine.

Mr K.
  • 1,064
  • 3
  • 19
  • 22