parameters: (argv)
docstring: Run the PNG encoder with options from the command line.
docstring_summary: Run the PNG encoder with options from the command line.
docstring_tokens: Run the PNG encoder with options from the command line .
function: def _main(argv):
      """
      Run the PNG encoder with options from the command line.
      """
  
      # Parse command line arguments
      from optparse import OptionParser
      import re
      version = '%prog ' + re.sub(r'( ?\$|URL: |Rev:)', '', __version__)
      parser = OptionParser(version=version)
      parser.set_usage("%prog [options] [imagefile]")
      parser.add_option('-r', '--read-png', default=False,
                        action='store_true',
                        help='Read PNG, write PNM')
      parser.add_option("-i", "--interlace",
                        default=False, action="store_true",
                        help="create an interlaced PNG file (Adam7)")
      parser.add_option("-t", "--transparent",
                        action="store", type="string", metavar="color",
                        help="mark the specified colour (#RRGGBB) as transparent")
      parser.add_option("-b", "--background",
                        action="store", type="string", metavar="color",
                        help="save the specified background colour")
      parser.add_option("-a", "--alpha",
                        action="store", type="string", metavar="pgmfile",
                        help="alpha channel transparency (RGBA)")
      parser.add_option("-g", "--gamma",
                        action="store", type="float", metavar="value",
                        help="save the specified gamma value")
      parser.add_option("-c", "--compression",
                        action="store", type="int", metavar="level",
                        help="zlib compression level (0-9)")
      parser.add_option("-T", "--test",
                        default=False, action="store_true",
                        help="create a test image (a named PngSuite image if an argument is supplied)")
      parser.add_option('-L', '--list',
                        default=False, action='store_true',
                        help="print list of named test images")
      parser.add_option("-R", "--test-red",
                        action="store", type="string", metavar="pattern",
                        help="test pattern for the red image layer")
      parser.add_option("-G", "--test-green",
                        action="store", type="string", metavar="pattern",
                        help="test pattern for the green image layer")
      parser.add_option("-B", "--test-blue",
                        action="store", type="string", metavar="pattern",
                        help="test pattern for the blue image layer")
      parser.add_option("-A", "--test-alpha",
                        action="store", type="string", metavar="pattern",
                        help="test pattern for the alpha image layer")
      parser.add_option("-K", "--test-black",
                        action="store", type="string", metavar="pattern",
                        help="test pattern for greyscale image")
      parser.add_option("-d", "--test-depth",
                        default=8, action="store", type="int",
                        metavar='NBITS',
                        help="create test PNGs that are NBITS bits per channel")
      parser.add_option("-S", "--test-size",
                        action="store", type="string", metavar="w[,h]",
                        help="width and height of the test image")
      (options, args) = parser.parse_args(args=argv[1:])
  
      # Convert options
      if options.transparent is not None:
          options.transparent = color_triple(options.transparent)
      if options.background is not None:
          options.background = color_triple(options.background)
  
      if options.list:
          names = list(_pngsuite)
          names.sort()
          for name in names:
              print(name)
          return
  
      # Run regression tests
      if options.test:
          return test_suite(options, args)
  
      # Prepare input and output files
      if len(args) == 0:
          infilename = '-'
          infile = sys.stdin
      elif len(args) == 1:
          infilename = args[0]
          infile = open(infilename, 'rb')
      else:
          parser.error("more than one input file")
      outfile = sys.stdout
      if sys.platform == "win32":
          import msvcrt, os
          msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
  
      if options.read_png:
          # Encode PNG to PPM
          png = Reader(file=infile)
          width,height,pixels,meta = png.asDirect()
          write_pnm(outfile, width, height, pixels, meta) 
      else:
          # Encode PNM to PNG
          format, width, height, depth, maxval = \
            read_pnm_header(infile, ('P5','P6','P7'))
          # When it comes to the variety of input formats, we do something
          # rather rude.  Observe that L, LA, RGB, RGBA are the 4 colour
          # types supported by PNG and that they correspond to 1, 2, 3, 4
          # channels respectively.  So we use the number of channels in
          # the source image to determine which one we have.  We do not
          # care about TUPLTYPE.
          greyscale = depth <= 2
          pamalpha = depth in (2,4)
          supported = map(lambda x: 2**x-1, range(1,17))
          try:
              mi = supported.index(maxval)
          except ValueError:
              raise NotImplementedError(
                'your maxval (%s) not in supported list %s' %
                (maxval, str(supported)))
          bitdepth = mi+1
          writer = Writer(width, height,
                          greyscale=greyscale,
                          bitdepth=bitdepth,
                          interlace=options.interlace,
                          transparent=options.transparent,
                          background=options.background,
                          alpha=bool(pamalpha or options.alpha),
                          gamma=options.gamma,
                          compression=options.compression)
          if options.alpha:
              pgmfile = open(options.alpha, 'rb')
              format, awidth, aheight, adepth, amaxval = \
                read_pnm_header(pgmfile, 'P5')
              if amaxval != '255':
                  raise NotImplementedError(
                    'maxval %s not supported for alpha channel' % amaxval)
              if (awidth, aheight) != (width, height):
                  raise ValueError("alpha channel image size mismatch"
                                   " (%s has %sx%s but %s has %sx%s)"
                                   % (infilename, width, height,
                                      options.alpha, awidth, aheight))
              writer.convert_ppm_and_pgm(infile, pgmfile, outfile)
          else:
              writer.convert_pnm(infile, outfile)
function_tokens: def _main ( argv ) : # Parse command line arguments from optparse import OptionParser import re version = '%prog ' + re . sub ( r'( ?\$|URL: |Rev:)' , '' , __version__ ) parser = OptionParser ( version = version ) parser . set_usage ( "%prog [options] [imagefile]" ) parser . add_option ( '-r' , '--read-png' , default = False , action = 'store_true' , help = 'Read PNG, write PNM' ) parser . add_option ( "-i" , "--interlace" , default = False , action = "store_true" , help = "create an interlaced PNG file (Adam7)" ) parser . add_option ( "-t" , "--transparent" , action = "store" , type = "string" , metavar = "color" , help = "mark the specified colour (#RRGGBB) as transparent" ) parser . add_option ( "-b" , "--background" , action = "store" , type = "string" , metavar = "color" , help = "save the specified background colour" ) parser . add_option ( "-a" , "--alpha" , action = "store" , type = "string" , metavar = "pgmfile" , help = "alpha channel transparency (RGBA)" ) parser . add_option ( "-g" , "--gamma" , action = "store" , type = "float" , metavar = "value" , help = "save the specified gamma value" ) parser . add_option ( "-c" , "--compression" , action = "store" , type = "int" , metavar = "level" , help = "zlib compression level (0-9)" ) parser . add_option ( "-T" , "--test" , default = False , action = "store_true" , help = "create a test image (a named PngSuite image if an argument is supplied)" ) parser . add_option ( '-L' , '--list' , default = False , action = 'store_true' , help = "print list of named test images" ) parser . add_option ( "-R" , "--test-red" , action = "store" , type = "string" , metavar = "pattern" , help = "test pattern for the red image layer" ) parser . add_option ( "-G" , "--test-green" , action = "store" , type = "string" , metavar = "pattern" , help = "test pattern for the green image layer" ) parser . add_option ( "-B" , "--test-blue" , action = "store" , type = "string" , metavar = "pattern" , help = "test pattern for the blue image layer" ) parser . add_option ( "-A" , "--test-alpha" , action = "store" , type = "string" , metavar = "pattern" , help = "test pattern for the alpha image layer" ) parser . add_option ( "-K" , "--test-black" , action = "store" , type = "string" , metavar = "pattern" , help = "test pattern for greyscale image" ) parser . add_option ( "-d" , "--test-depth" , default = 8 , action = "store" , type = "int" , metavar = 'NBITS' , help = "create test PNGs that are NBITS bits per channel" ) parser . add_option ( "-S" , "--test-size" , action = "store" , type = "string" , metavar = "w[,h]" , help = "width and height of the test image" ) ( options , args ) = parser . parse_args ( args = argv [ 1 : ] ) # Convert options if options . transparent is not None : options . transparent = color_triple ( options . transparent ) if options . background is not None : options . background = color_triple ( options . background ) if options . list : names = list ( _pngsuite ) names . sort ( ) for name in names : print ( name ) return # Run regression tests if options . test : return test_suite ( options , args ) # Prepare input and output files if len ( args ) == 0 : infilename = '-' infile = sys . stdin elif len ( args ) == 1 : infilename = args [ 0 ] infile = open ( infilename , 'rb' ) else : parser . error ( "more than one input file" ) outfile = sys . stdout if sys . platform == "win32" : import msvcrt , os msvcrt . setmode ( sys . stdout . fileno ( ) , os . O_BINARY ) if options . read_png : # Encode PNG to PPM png = Reader ( file = infile ) width , height , pixels , meta = png . asDirect ( ) write_pnm ( outfile , width , height , pixels , meta ) else : # Encode PNM to PNG format , width , height , depth , maxval = read_pnm_header ( infile , ( 'P5' , 'P6' , 'P7' ) ) # When it comes to the variety of input formats, we do something # rather rude.  Observe that L, LA, RGB, RGBA are the 4 colour # types supported by PNG and that they correspond to 1, 2, 3, 4 # channels respectively.  So we use the number of channels in # the source image to determine which one we have.  We do not # care about TUPLTYPE. greyscale = depth <= 2 pamalpha = depth in ( 2 , 4 ) supported = map ( lambda x : 2 ** x - 1 , range ( 1 , 17 ) ) try : mi = supported . index ( maxval ) except ValueError : raise NotImplementedError ( 'your maxval (%s) not in supported list %s' % ( maxval , str ( supported ) ) ) bitdepth = mi + 1 writer = Writer ( width , height , greyscale = greyscale , bitdepth = bitdepth , interlace = options . interlace , transparent = options . transparent , background = options . background , alpha = bool ( pamalpha or options . alpha ) , gamma = options . gamma , compression = options . compression ) if options . alpha : pgmfile = open ( options . alpha , 'rb' ) format , awidth , aheight , adepth , amaxval = read_pnm_header ( pgmfile , 'P5' ) if amaxval != '255' : raise NotImplementedError ( 'maxval %s not supported for alpha channel' % amaxval ) if ( awidth , aheight ) != ( width , height ) : raise ValueError ( "alpha channel image size mismatch" " (%s has %sx%s but %s has %sx%s)" % ( infilename , width , height , options . alpha , awidth , aheight ) ) writer . convert_ppm_and_pgm ( infile , pgmfile , outfile ) else : writer . convert_pnm ( infile , outfile )