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