2

I have a bunch of PNGs that I want to turn into an animation. I use matplotlib for this. I can display the resulting animation on the screen and everything looks fine. But the saved MP4 is just a blank. I mean I can play it, but it just shows a white, featureless window. Any ideas what I'm doing wrong? Here's the code:

import matplotlib
matplotlib.use('TKAgg')
import pylab as pyl
import matplotlib.pyplot as pplt
import matplotlib.image as mplimg
import matplotlib.animation as mplanim

myimages = []

for k in range(1,100):
    fname = "data{0:03d}.png".format(k)
    img = mplimg.imread(fname)
    imgplot = pplt.imshow(img)
    myimages.append([imgplot])

fig = pyl.figure()

myanim = mplanim.ArtistAnimation(fig, myimages, interval=20,
                                  blit=True, repeat_delay=1000)


myanim.save("anim.mp4", fps=10)

pyl.show()

UPDATE: Moving fig = pyl.figure() to the top of the code, right after the imports solves the problem. If anyone knows why, feel free to tell! Thanks.

bob.sacamento
  • 6,283
  • 10
  • 56
  • 115
  • Try setting `blit=False`. – Mike Müller Dec 01 '15 at 19:59
  • @Mike Muller Thanks, but the result is the same. – bob.sacamento Dec 01 '15 at 20:03
  • Just use [FFmpeg](https://trac.ffmpeg.org/wiki/Create%20a%20video%20slideshow%20from%20images) directly `ffmpeg -framerate 1 -i data%03d.png -r 10 ` – Mike Müller Dec 01 '15 at 20:13
  • between this and your last question I suspect you want to be using `FuncAnimation` on what ever code is generating the images you are trying to stitch together here. – tacaswell Dec 01 '15 at 21:43
  • 1
    `fig = pyl.figure()`creates a new figure while `pplt.imshow(img)` added the image to the current plot axes that belongs to the default figure. Moving `fig = pyl.figure()` to the front makes it the default figure. – Mike Müller Dec 01 '15 at 22:11

2 Answers2

0

BTW, I face a similar problem on "matplotlib animation produces a blank".

Sometimes the matplotlib version can also lead to the blank outputs.

I solve it by downgrade from 3.7.1 to 3.4.1

In particular, it shows ``ax.lines this Attribute can not be set''. (I guess it maybe removed in the latest version?)

-2

Correct this line:

myimages.append([imgplot])

into:

myimages.append(imgplot)

and see if it works.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • Thanks for all your efforts. However, it didn't like that at all. ArtistAnimation returns an error "TypeError: 'AxesImage' object is not iterable" – bob.sacamento Dec 01 '15 at 20:18