OLD | NEW |
(Empty) | |
| 1 ''' |
| 2 convert an image to an rgba array |
| 3 |
| 4 Launch with --help to see more information. |
| 5 |
| 6 Copyright 2013 Google Inc. |
| 7 |
| 8 Use of this source code is governed by a BSD-style license that can be |
| 9 found in the LICENSE file. |
| 10 ''' |
| 11 |
| 12 import Image |
| 13 import optparse |
| 14 import sys |
| 15 |
| 16 def DumpImage(filename, write_path, variable_name): |
| 17 if not write_path: |
| 18 outfile = sys.stdout |
| 19 else: |
| 20 try: |
| 21 outfile = open( write_path, 'w' ) |
| 22 except: |
| 23 raise BaseException( "Couldn't open output path %s" % write_path ) |
| 24 |
| 25 infile = Image.open( filename ) |
| 26 |
| 27 image_size = infile.size |
| 28 |
| 29 coordinates = [(x,y) for y in range(image_size[1]) for x in range(image_size[0
])] |
| 30 |
| 31 colors = map( lambda xy: infile.getpixel(xy) + (255,) , coordinates ) |
| 32 values = [str(value) for color in colors for value in color] |
| 33 |
| 34 print >> outfile, "unsigned char %s[] = {" % variable_name |
| 35 print >> outfile, ', '.join(values) |
| 36 print >> outfile, "};" |
| 37 |
| 38 if write_path: |
| 39 outfile.close() |
| 40 |
| 41 def Main(options, args): |
| 42 num_args = len(args) |
| 43 if num_args != 1: |
| 44 raise BaseException("missing image filename") |
| 45 DumpImage(args[0], write_path=options.write_path, variable_name=options.variab
le_name) |
| 46 |
| 47 if __name__ == '__main__': |
| 48 parser = optparse.OptionParser(usage=r"usage: %prog [options] imagefile") |
| 49 parser.add_option('-w', '--write-path', |
| 50 action='store', type='string', default='', |
| 51 help='Path to write the output scanline [default stdout]') |
| 52 parser.add_option('-v', '--variable-name', |
| 53 action='store', type='string', default='image_bytes', |
| 54 help=r'Path to write the output scanline [default %default
]') |
| 55 |
| 56 (options, args) = parser.parse_args() |
| 57 Main(options, args) |
OLD | NEW |