On Wed, Jul 29, 2009 at 6:09 PM, John Hunter <jdh2358@gmail.com> wrote:
On Wed, Jul 29, 2009 at 6:50 PM, Art<grenander@gmail.com> wrote:

> My bottleneck now is actually saving the pngs for mencoder to make into an
> avi (as with the movie_demo example on your site) using savefig. Do you also
> know of an alternative way of generating an avi of the animation? My
> animation has about 9000 frames.

No, I can't imagine there is much fat to trim in that part of the
code.  We're using libpng to write the png, so no speedups there
unless you can make the pngs smaller.  It looks like we are using a
memory pointer to get the rgba data from agg over to png, so no
speedups there either.  Perhaps Michael has some input.  Some code
that we could run and test might help produce some further
optimizations.

JDH

Below is some sample code that creates the directory ~/tmp/blit_test, outputs 50 pngs, and creates a mov.avi using /usr/local/bin/mencoder. Apologies in advance for the code:

import os
import numpy as np
import matplotlib.pyplot as plt
import subprocess

movdir = os.path.join(os.path.expanduser('~'), 'tmp', 'blit_test')
try: os.mkdirs(movdir)
except: pass
mencoder = '/usr/local/bin/mencoder'

ax = []
im = []
vl = []

fig = plt.figure()

ax.append(fig.add_subplot(2,2,1))
im.append(ax[-1].imshow(np.random.randn(100,100)))
ax[-1].set_xticks([])
ax[-1].set_yticks([])

ax.append(fig.add_subplot(2,2,2))
im.append(ax[-1].imshow(np.random.randn(32,32)))
ax[-1].set_xticks([])
ax[-1].set_yticks([])

ax.append(fig.add_subplot(2, 1, 2))

r = np.abs(np.random.normal(0,0.1,1000))
for i in range(100):
    ras = np.nonzero(np.random.poisson(r))[0]
    ax[-1].scatter(ras, np.ones(len(ras)) * (i+1), s=1, alpha=0.5)
   
vl.append(ax[-1].axvline(-1, color='r', alpha=0.9, linewidth=2.))
ax[-1].set_xlim([0,1000])
ax[-1].set_ylim([0,100])
plt.draw()

canvas = fig.canvas
background = canvas.copy_from_bbox(fig.bbox)

for i in range(50):
    canvas.restore_region(background)
    im[0].set_data(np.random.randn(100,100))
    ax[0].draw_artist(im[0])
   
    im[1].set_data(np.random.randn(32,32))
    ax[1].draw_artist(im[1])
   
    vl[0].set_xdata([i,i])
    ax[2].draw_artist(vl[0])
   
    canvas.blit()
    plt.savefig(os.path.join(movdir, '%03d' % (i+1)))

fps = 10
   
command = (mencoder,
           'mf://%s/*.png' % (movdir),
           #'-vf', 'scale=800:-10',
           '-mf', 'fps=%d' % fps,
           '-ovc', 'lavc',
           '-lavcopts', 'vcodec=mpeg4',
           '-o', os.path.join(movdir, 'mov.avi'))
subprocess.check_call(command)

# output in ~/tmp/blit_test/mov.avi