1 #!/usr/bin/env python 2 3 """ 4 Produce program files for image data converted to 16bpp values suitable for 5 writing directly to the display. 6 7 Copyright (C) 2013 Paul Boddie 8 9 This program is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 2 of the License, or 12 (at your option) any later version. 13 """ 14 15 import PIL.Image 16 import math 17 import sys 18 19 def get_image(input_filename, width, height): 20 21 i = PIL.Image.open(input_filename) 22 i.thumbnail((width, height), PIL.Image.ANTIALIAS) 23 24 all_values = [] 25 values = [] 26 last = None 27 x = 0 28 for (r, g, b) in i.getdata(): 29 rgb = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3) 30 values.append(rgb) 31 x += 1 32 33 if x == width: 34 x = 0 35 all_values.append(values) 36 values = [] 37 38 return all_values 39 40 def get_image_size(width, height): 41 return width * height 42 43 if __name__ == "__main__": 44 import sys, os 45 46 if len(sys.argv) < 3: 47 print sys.argv[0], "<output basename> <image filename>..." 48 sys.exit(1) 49 50 output_basename = sys.argv[1] 51 input_filenames = sys.argv[2:] 52 width = 132 53 height = 132 54 size = get_image_size(width, height) 55 number_of_images = len(input_filenames) 56 57 f = open("%s.c" % output_basename, "wb") 58 59 try: 60 # Write the image data. 61 62 f.write("#include <stdint.h>\n") 63 f.write("const uint16_t image_data[%d][%d] = {\n" % (number_of_images, size)) 64 for input_filename in input_filenames: 65 f.write("{\n") 66 for values in get_image(input_filename, width, height): 67 f.write(", ".join([("0x%04x" % v) for v in values])) 68 f.write(",\n") 69 f.write("},\n") 70 f.write("};\n") 71 72 finally: 73 f.close() 74 75 f = open("%s.h" % output_basename, "wb") 76 77 try: 78 f.write("#include <stdint.h>\n") 79 f.write("extern const uint16_t image_data[%d][%d];\n" % (number_of_images, size)) 80 f.write("#define MAX_IMAGE %d\n" % (number_of_images - 1)) 81 f.write("#define NUMBER_OF_IMAGES %d\n" % number_of_images) 82 f.write("#define IMAGE_SIZE %d\n" % size) 83 f.write("#define IMAGE_WIDTH %d\n" % width) 84 f.write("#define IMAGE_HEIGHT %d\n" % height) 85 86 finally: 87 f.close() 88 89 # vim: tabstop=4 expandtab shiftwidth=4