#!/usr/bin/env python # convert a file of gnuplot commands to a gif file. # Unfortunately, gif output is not available in gnuplot, so gnuplot is # used to write the plot image to another format (pbm or png), then the # Python Image Library is used to convert it to gif. # # Updated July 19, 2006, BB import sys, os import Image helpmsg = """Usage: gnu2gif.py plotfile [-x xsize -y ysize -c color -f font] size = 1 means use default size (640x480) color can be 1 (use color) or zero (black and white) font can be s,m,l (small, medium, large) Defaults: x,ysize=1 (640x480), color=1, font=m Examples: gnu2gif.py plotfile -c 0 -f s gives default size (640x480), monochrome, with small font gnu2gif.py plotfile -x 1.2 -y 0.8 gives 1.2*640, 0.8*480 = size 768x384 input plot001 gives output plot001.gif """ # default size is 640 x 480 (for size_x = size_y = 1.0) # Alter size variables to reflect proportion of default size # e.g. -x 1.2 -y 0.8 gives 768 x 384 pixels (total image, not plot interior) size_x = 1.0 size_y = 1.0 color = 1 # set to 1 = use color; set to zero = use black & white font = 'medium' # 'small', 'medium' or 'large' def processargs(arglist): " eg. processargs [-d database -uuser -ppassword -t table_name] " D = {} while 1: if len(arglist) < 1: return D a = arglist[0] if a[0] == '-': if len(a) == 2: if len(arglist) < 2: return D D[a] = arglist[1] arglist = arglist[2:] elif len(a) > 2: flag = a[0:2] val = a[2:] D[flag] = val arglist = arglist[1:] else: if 'noflag' in D: D['noflag'].append(a) else: D['noflag'] = [a] arglist = arglist[1:] def exit(): print helpmsg sys.exit(0) if __name__ == '__main__': nargs = len(sys.argv[1:]) if nargs == 0: exit() # process the arguments argdict = processargs(sys.argv[1:]) if 'noflag' not in argdict: exit() else: inplot = argdict['noflag'][0] # noflag is a list if not os.path.exists(inplot): print "Unable to find %s" % inplot sys.exit() if '-x' in argdict: size_x = float(argdict['-x']) if '-y' in argdict: size_y = float(argdict['-y']) if '-c' in argdict: color = int(argdict['-c']) if '-f' in argdict: f = argdict['-f'].lower() if f[0] == 's': font = 'small' elif f[0] == 'm': font = 'medium' elif f[0] == 'l': font = 'large' base,ext = os.path.splitext(inplot) outplot = base + ".gif" if color > 0: cbw = 'color' else: cbw = 'monochrome' # png looks better, but not available on Irix(?) if sys.platform[:4] == 'irix': platform = 'irix' filetype = "pbm" else: platform = 'linux' filetype = "png" # create the gnuplot file that will write out an image tmpname = 'tmp66tg65.' + filetype f = open(tmpname, 'w') f.write("set size %f,%f\n" % (size_x, size_y)) f.write("set terminal %s %s %s\n" % (filetype, cbw, font)) f.write("set output '%s'\n" % (tmpname)) f.close() cmd = 'gnuplot %s %s' % (tmpname,inplot) os.system(cmd) # call gnuplot # convert the temporary file to gif format Image.open(tmpname).save(outplot) print "output written to %s" % outplot os.remove(tmpname)