#encoding:latin-1 from numpy import * from pylab import * import sys, os, glob, textwrap from pgm_utils import * ############################################################################### ### ### creates movie with several rectangles fading in and out ### ### frames are stored in pgm format below directory 'movie_dir'. ### ### if imagemagick is avaliable the program builds an animated ### gif. ### ############################################################################### M,N = 70, 70 movie_dir="movie/" try: os.mkdir(movie_dir) except Exception, dir_exists: pass def draw_rectangle(img, val, x0, y0, w, h): for x in range(x0, x0+w): for y in range(y0, y0+h): img[x,y] = val return img #### configure animated objects ############################################## # illumination increases from color_min to 1 in time [tmin, tmid] # and decreases from 1 to color_min in time [tmid, tmax] # tmid is (tmin+tmax)/2.0 color_min = 0.1 # no black rectangles ! object_data = [ # x0 y0 w h tmin tmax ( (10, 10, 20, 20), (0, 20) ), ( (20, 20, 20, 20), (5, 25) ), ( (30, 30, 20, 20), (10, 30) ), ( (40, 40, 20, 20), (15, 35) ), ] #### draw and save every frame ################################################ tmax = max( t for (_, (_,t)) in object_data) for t in range(tmax): print "frame %3d of %d" % (t+1, tmax) img = zeros((M,N), float) for dimensions, (tmin, tmax) in object_data: if t < tmin or t>tmax: continue tmid = (tmin+tmax)/2.0 if t < tmid: val = (t-tmin)/(tmid-tmin) else: val = (tmax-t)/(tmax-tmid) val = color_min + (1.0-color_min)*val draw_rectangle(img, val, *dimensions) save_pgm(os.path.join(movie_dir, "frame%03d.pgm" % t), img) print print "seperate images written to", movie_dir print #### try to call imagemagics convert commandlinetool for building ############# #### animated gif print "try to make animated gif:", try: os.system("convert -delay 20 -loop 0 %s movie.gif" % os.path.join(movie_dir,"frame*.pgm")) except Exception, convert_does_not_exist: print "failed to call imagemagick" else: print "animated movie.gif written"