3 # Copyright (C) 2014 Antonio Ospite <ao2@ao2.it>
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 def usage(program_name):
23 sys.stderr.write("usage: %s <image file> <animation file>\n" % program_name)
27 # The code below has been copied from the C implementation in PatternPaint:
28 # https://github.com/Blinkinlabs/PatternPaint
29 def save_animation(input_filename, output_filename):
30 output_file = open(output_filename, "w")
32 img = Image.open(input_filename).convert('RGB')
34 colors = img.getcolors(w*h)
35 palette = img.getpalette()
36 pixels = img.getdata()
38 output_file.write("const PROGMEM prog_uint8_t animationData[] = {\n")
40 output_file.write("// Length of the color table - 1, in bytes. length: 1 byte\n")
41 output_file.write(" %d,\n" % (len(colors) - 1))
43 output_file.write("// Color table section. Each entry is 3 bytes. length: %d bytes\n" % (len(colors) * 3))
46 for i, c in enumerate(colors):
47 output_file.write(" %3d, %3d, %3d,\n" % (c[1][0], c[1][1], c[1][2]))
50 output_file.write("// Pixel runs section. Each pixel run is 2 bytes. length: -1 bytes\n")
55 new_color = color_map[pixels.getpixel((x, y))]
57 current_color = new_color
59 if current_color != new_color:
60 output_file.write(" %3d, %3d,\n" % (run_count, current_color))
62 current_color = new_color
66 output_file.write(" %3d, %3d,\n" % (run_count, current_color))
68 output_file.write("};\n\n")
70 output_file.write("#define NUM_FRAMES %d\n" % w)
71 output_file.write("#define NUM_LEDS %d\n" % h)
72 output_file.write("Animation animation(NUM_FRAMES, animationData, NUM_LEDS);\n")
75 if __name__ == "__main__":
79 save_animation(sys.argv[1], sys.argv[2])