Index: tools/image_to_byte_array.py |
diff --git a/tools/image_to_byte_array.py b/tools/image_to_byte_array.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..71b5119231ad0b29f0666521e8c8530980218f3e |
--- /dev/null |
+++ b/tools/image_to_byte_array.py |
@@ -0,0 +1,57 @@ |
+''' |
+convert an image to an rgba array |
+ |
+Launch with --help to see more information. |
+ |
+Copyright 2013 Google Inc. |
+ |
+Use of this source code is governed by a BSD-style license that can be |
+found in the LICENSE file. |
+''' |
+ |
+import Image |
+import optparse |
+import sys |
+ |
+def DumpImage(filename, write_path, variable_name): |
+ if not write_path: |
+ outfile = sys.stdout |
+ else: |
+ try: |
+ outfile = open( write_path, 'w' ) |
+ except: |
+ raise BaseException( "Couldn't open output path %s" % write_path ) |
+ |
+ infile = Image.open( filename ) |
+ |
+ image_size = infile.size |
+ |
+ coordinates = [(x,y) for y in range(image_size[1]) for x in range(image_size[0])] |
+ |
+ colors = map( lambda xy: infile.getpixel(xy) + (255,) , coordinates ) |
+ values = [str(value) for color in colors for value in color] |
+ |
+ print >> outfile, "unsigned char %s[] = {" % variable_name |
+ print >> outfile, ', '.join(values) |
+ print >> outfile, "};" |
+ |
+ if write_path: |
+ outfile.close() |
+ |
+def Main(options, args): |
+ num_args = len(args) |
+ if num_args != 1: |
+ raise BaseException("missing image filename") |
+ DumpImage(args[0], write_path=options.write_path, variable_name=options.variable_name) |
+ |
+if __name__ == '__main__': |
+ parser = optparse.OptionParser(usage=r"usage: %prog [options] imagefile") |
+ parser.add_option('-w', '--write-path', |
+ action='store', type='string', default='', |
+ help='Path to write the output scanline [default stdout]') |
+ parser.add_option('-v', '--variable-name', |
+ action='store', type='string', default='image_bytes', |
+ help=r'Path to write the output scanline [default %default]') |
+ |
+ (options, args) = parser.parse_args() |
+ Main(options, args) |