home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pypil112.zip / Scripts / explode.py < prev    next >
Text File  |  2001-05-03  |  2KB  |  108 lines

  1. #
  2. # The Python Imaging Library
  3. # $Id$
  4. #
  5. # split an animation into a number of frame files
  6. #
  7.  
  8. import Image
  9. import os, string, sys
  10.  
  11. class Interval:
  12.  
  13.     def __init__(self, interval = "0"):
  14.  
  15.         self.setinterval(interval)
  16.  
  17.     def setinterval(self, interval):
  18.  
  19.         self.hilo = []
  20.  
  21.         for s in string.split(interval, ","):
  22.             if not string.strip(s):
  23.                 continue
  24.             try:
  25.                 v = string.atoi(s)
  26.                 if v < 0:
  27.                     lo, hi = 0, -v
  28.                 else:
  29.                     lo = hi = v
  30.             except ValueError:
  31.                 i = string.find(s, "-")
  32.                 lo, hi = string.atoi(s[:i]), string.atoi(s[i+1:])
  33.  
  34.             self.hilo.append((hi, lo))
  35.  
  36.         if not self.hilo:
  37.             self.hilo = [(sys.maxint, 0)]
  38.  
  39.     def __getitem__(self, index):
  40.  
  41.         for hi, lo in self.hilo:
  42.             if hi >= index >= lo:
  43.                 return 1
  44.         return 0
  45.  
  46. # --------------------------------------------------------------------
  47. # main program
  48.  
  49. html = 0
  50.  
  51. if sys.argv[1:2] == ["-h"]:
  52.     html = 1
  53.     del sys.argv[1]
  54.  
  55. if not sys.argv[2:]:
  56.     print
  57.     print "Syntax: python explode.py infile template [range]"
  58.     print
  59.     print "The template argument is used to construct the names of the"
  60.     print "individual frame files.  The frames are numbered file001.ext,"
  61.     print "file002.ext, etc.  You can insert %d to control the placement"
  62.     print "and syntax of the frame number."
  63.     print
  64.     print "The optional range argument specifies which frames to extract."
  65.     print "You can give one or more ranges like 1-10, 5, -15 etc.  If"
  66.     print "omitted, all frames are extracted."
  67.     sys.exit(1)
  68.  
  69. infile = sys.argv[1]
  70. outfile = sys.argv[2]
  71.  
  72. frames = Interval(string.join(sys.argv[3:], ","))
  73.  
  74. try:
  75.     # check if outfile contains a placeholder
  76.     outfile % 1
  77. except TypeError:
  78.     file, ext = os.path.splitext(outfile)
  79.     outfile = file + "%03d" + ext
  80.  
  81. ix = 1
  82.  
  83. im = Image.open(infile)
  84.  
  85. if html:
  86.     file, ext = os.path.splitext(outfile)
  87.     html = open(file+".html", "w")
  88.     html.write("<html>\n<body>\n")
  89.  
  90. while 1:
  91.  
  92.     if frames[ix]:
  93.         im.save(outfile % ix)
  94.         print outfile % ix
  95.  
  96.         if html:
  97.             html.write("<img src='%s'><br>\n" % outfile % ix)
  98.  
  99.     try:
  100.         im.seek(ix)
  101.     except EOFError:
  102.         break
  103.  
  104.     ix = ix + 1
  105.  
  106. if html:
  107.     html.write("</body>\n</html>\n")
  108.