[Review for External] Using WRITE_GIF to create GIFs in IDL
Anonym

Creating GIFs with function graphics can be tricky if you want to use WRITE_GIF. This is because you have to convert the color scale of the image to be 8-bit (256 colors total). The function COLOR_QUAN can be used to convert a function graphic's window contents to an 8 bit color image. COLOR_QUAN also returns the associated RGB vectors for the image, which are needed when using WRITE_GIF. The code below was used to generate the animation that is above for a shooting star. Save the example below as gif_with_color_quan.pro and then you can compile and run from the IDL Workbench. The GIF shown above will be generated and saved in the same directory as the .pro code.
pro gif_with_color_quan
compile_opt idl2
ireset, /no_prompt
;find the current directory
scope = SCOPE_TRACEBACK(/STRUCT)
thisfilename = (scope[-1]).FILENAME
thisdir = FILE_DIRNAME(thisfilename)
;set the output file
outfile = thisdir + path_sep() + 'sample_gif.gif'
;set some animation information
nframes = 30
xrange =[0,nframes-1]
yrange = [0,nframes-1]
;initialize plot and window
w = window()
p = scatterplot(0,0, /current,SYMBOL='star',$
xrange = xrange, yrange = yrange,$
SYM_COLOR = 'gold', SYM_SIZE = 4,$
/sym_filled)
;initialize data trail
npoints = 10
trail_length = 3
max_spread = 3 ;at length
trailx = make_array(npoints, /float)
traily = make_array(npoints, /float)
p2 = scatterplot(trailx, traily, /current,/overplot, $
SYM_COLOR = 'red', /sym_filled)
;iterate through the frames
for i=0,nframes-1 do begin
;update the data points
p.setdata, i, i
;generate a trail
p.getdata, starx, stary
for j=0, npoints-1 do begin
trailx[j] = starx-.3*trail_length - (randomu(seed, 1))[0]*trail_length
traily[j] = trailx[j] + (2*randomu(seed, 1)-1)[0]*max_spread*((starx -$
trailx[j])/trail_length)
endfor
p2.setdata, trailx, traily
;save our image frame
img = w.CopyWindow()
;convert the image to 8 bit
img = color_quan(img, 1, r, g, b)
;duplicate frames otherwise low frame-rate can be a problem with some players
WRITE_GIF, outfile, img, r, g, b, delay_time = 20 ,$
/multiple, repeat_count = 0
;output some important processing information to the IDL Console
print, 'Generated ' + strtrim(i+1,1) + ' of '+ strtrim(nframes,1) + ' frames'
endfor
print, ''
print, 'Video successfully generated! Saved to:'
print, outfile
print, ''
; Close the file and iwindow
WRITE_GIF, outpath, /CLOSE
END
reviewed by ZN 9/22/2015