#!/usr/bin/env python from __future__ import print_function '''makes html with javascript to animate a sequence of images''' __author__ = 'Brian Fiedler' # This module can be imported to access the function makeanim. # Or it can be run independently as a script from the command line. # Suppose the command "ls mydir/*.png" gives the list of files to be animated. # # python janim.py myimages/*.png > myanimator.html # # python janim.py -i listofimageurls.txt -o myanimator.html -t "rainfall animation" # # myanimator.html wll be viewable in a browser, but you may want to add more html to it. # Or you can embed myanimator.html within another web page, with html code like: # error message here # (The width and height in the above example is for 900x600 images, with controls at the bottom.) # # Alternative to using janim.py from the command line: # files = glob.glob("pngs/*.png") # janim.makeanim(files,outfile="myanimator.html",sortOrder=True,ctlOnSide=True,titlestring="rainfall animation") # # For a similar animator, but with more features, see http://www.ssec.wisc.edu/hanis/ def makeanim(files=[],ctlOnSide=False,revOrder=False,sortOrder=False, titlestring="animation",fileOfFileNames="",outfile=""): # files is a list of paths to the image files # possible to append more paths to files: if fileOfFileNames: # file with image file names, either on new lines or separated by white space urls = open(fileOfFileNames).read().split() files += [x.strip() for x in urls if x.strip()] if sortOrder: files.sort() if revOrder: files.reverse() nim=len(files) #number of image files #### some large template strings follow: top = """ %s """ form = """
  000
""" ### use the paths to images stored in files list to make javascript links to the images: imagecode = """aniFrames[%d] = new Image();\naniFrames[%d].src = "%s";\n""" imagepaths = "" for i in range(nim): imagepaths += imagecode % (i,i,files[i]) # show the first image, before animation replaces it: firstimgtag = 'your image should have been seen here!' % files[0] ### now put together the web page containing javascript and html for your animation: webpage = top % (titlestring,nim-1) webpage += imagepaths webpage += script webpage += "
\n" if ctlOnSide: #controls are on the left side of the image webpage += "
\n" webpage += form webpage += "\n" webpage += firstimgtag webpage += "
\n" else: #controls are below the image webpage += firstimgtag webpage += form webpage += "
\n" if outfile: # write out the html file ouf = open(outfile,'w') ouf.write(webpage) ouf.close() else: # written the html file contents as a string return webpage #### optionally process command line arguments for a call to makeanim if __name__ == '__main__': import argparse parser = argparse.ArgumentParser(description= "produces html with javascript for animation" ) parser.add_argument("-s","--side", dest="ctlOnSide",action="store_true",help="put controls on side") parser.add_argument("--sort", dest="sortOrder",action="store_true",help="sorts the image order") parser.add_argument("--rev", dest="revOrder",action="store_true",help="reverses the image order") parser.add_argument("-t","--title", dest="titlestring",type=str, help="title string in quotes",default="javascript animation") parser.add_argument("-i","--fof", dest="fileOfFileNames",type=str,help="name of file containing file urls",default="") parser.add_argument("-o","--outfile", dest="outfile",type=str,help="name of html output file",default="") parser.add_argument("files",help="paths to image files",nargs='*') args = parser.parse_args() if len(args.files) == 0: parser.print_help() else: webpage = makeanim(args.files,ctlOnSide=args.ctlOnSide,revOrder=args.revOrder,sortOrder=args.sortOrder, titlestring=args.titlestring, fileOfFileNames=args.fileOfFileNames,outfile=args.outfile) if not args.outfile: print(webpage)