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

  1. #! /usr/local/bin/python
  2. #
  3. # The Python Imaging Library.
  4. # $Id: pilconvert.py,v 1.1 1996/10/04 19:40:40 fredrik Exp $
  5. #
  6. # convert image files
  7. #
  8. # History:
  9. # 0.1   96-04-20 fl     Created
  10. # 0.2   96-10-04 fl     Use draft mode when converting images
  11. # 0.3   96-12-30 fl     Optimize output (PNG, JPEG)
  12. # 0.4   97-01-18 fl     Made optimize an option (PNG, JPEG)
  13. # 0.5   98-12-30 fl     Fixed -f option (from Anthony Baxter)
  14. #
  15.  
  16. import Image
  17.  
  18. import getopt, string, sys
  19.  
  20. def usage():
  21.     print "PIL Convert 0.5/98-12-30 -- convert image files"
  22.     print "Usage: pilconvert [option] infile outfile"
  23.     print
  24.     print "Options:"
  25.     print
  26.     print "  -c <format>  convert to format (default is given by extension)"
  27.     print
  28.     print "  -g           convert to greyscale"
  29.     print "  -p           convert to palette image (using standard palette)"
  30.     print "  -r           convert to rgb"
  31.     print
  32.     print "  -o           optimize output (trade speed for size)"
  33.     print "  -q <value>   set compression quality (0-100, JPEG only)"
  34.     print
  35.     print "  -f           list supported file formats"
  36.     sys.exit(1)
  37.  
  38. if len(sys.argv) == 1:
  39.     usage()
  40.  
  41. try:
  42.     opt, argv = getopt.getopt(sys.argv[1:], "c:dfgopq:r")
  43. except getopt.error, v:
  44.     print v
  45.     sys.exit(1)
  46.  
  47. format = None
  48. convert = None
  49.  
  50. options = { }
  51.  
  52. for o, a in opt:
  53.  
  54.     if o == "-f":
  55.         Image.init()
  56.         id = Image.ID[:]
  57.         id.sort()
  58.         print "Supported formats (* indicates output format):"
  59.         for i in id:
  60.             if Image.SAVE.has_key(i):
  61.                 print i+"*",
  62.             else:
  63.                 print i, 
  64.         sys.exit(1)
  65.  
  66.     elif o == "-c":
  67.         format = a
  68.  
  69.     if o == "-g":
  70.         convert = "L"
  71.     elif o == "-p":
  72.         convert = "P"
  73.     elif o == "-r":
  74.         convert = "RGB"
  75.  
  76.     elif o == "-o":
  77.         options["optimize"] = 1
  78.     elif o == "-q":
  79.         options["quality"] = string.atoi(a)
  80.  
  81. if len(argv) != 2:
  82.     usage()
  83.  
  84. try:
  85.     im = Image.open(argv[0])
  86.     if convert and im.mode != convert:
  87.         im.draft(convert, im.size)
  88.         im = im.convert(convert)
  89.     if format:
  90.         apply(im.save, (argv[1], format), options)
  91.     else:
  92.         apply(im.save, (argv[1],), options)
  93. except:
  94.     print "cannot convert image",
  95.     print "(%s:%s)" % (sys.exc_type, sys.exc_value)
  96.