Mercurial > louis > kiibohd-controller
annotate Scan/STLcd/bitmap2Struct.py @ 398:63b1d003fd50
Stop requiring editing of example scripts
The example scripts include hardcoded values that
do not work for everyone. Instead of requiring the
files to be edited (and dirtying the git tree),
allow them to take command-line arguments.
Also adds better guidance for Mac OSX virtual
serial ports.
author | Joshua Flanagan <joshuaflanagan@gmail.com> |
---|---|
date | Tue, 10 Nov 2015 10:32:06 -0600 |
parents | 2e0074f75855 |
children | 3437e2246259 |
rev | line source |
---|---|
332
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
2 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
3 # Copyright (C) 2015 by Jacob Alexander |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
4 # |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
5 # This file is free software: you can redistribute it and/or modify |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
6 # it under the terms of the GNU General Public License as published by |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
7 # the Free Software Foundation, either version 3 of the License, or |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
8 # (at your option) any later version. |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
9 # |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
10 # This file is distributed in the hope that it will be useful, |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
13 # GNU General Public License for more details. |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
14 # |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
15 # You should have received a copy of the GNU General Public License |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
16 # along with this file. If not, see <http://www.gnu.org/licenses/>. |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
17 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
18 # Imports |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
19 import sys |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
20 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
21 from array import * |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
22 from PIL import Image |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
23 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
24 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
25 # Convenience class to deal with converting images to a C array |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
26 class STLcdGraphic: |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
27 # Some constants for the LCD Driver |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
28 page_width = 8 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
29 page_max_length = 132 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
30 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
31 array('B') |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
32 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
33 def __init__( self, height, width ): |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
34 self.height = height |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
35 self.width = width |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
36 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
37 # Calculate number of pages |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
38 self.page_count = int( self.height / self.page_width ) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
39 self.page_length = self.width |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
40 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
41 # Initialize pages to 0's |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
42 self.page_data = [] |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
43 for page in range( 0, self.page_count ): |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
44 self.page_data.append( array( 'B', [0] * self.page_length ) ) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
45 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
46 def setpixel( self, x, y ): |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
47 # Calculate which page |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
48 page = int( ( self.height - y ) / self.page_width ) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
49 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
50 if page == 4: |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
51 print("YAR", (x,y)) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
52 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
53 # Calculate which byte |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
54 byte = x |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
55 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
56 # Calculate which bit |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
57 bit = int( ( self.height - y ) % self.page_width ) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
58 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
59 # Set pixel bit |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
60 self.page_data[ page ][ byte ] |= (1 << bit) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
61 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
62 def getpage( self, page ): |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
63 return self.page_data[ page ] |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
64 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
65 def getarray( self ): |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
66 struct = "{\n" |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
67 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
68 for page in range( 0, self.page_count ): |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
69 for elem in self.page_data[ page ]: |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
70 struct += "0x{0:02x}, ".format( elem ) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
71 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
72 if page != self.page_count - 1: |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
73 struct += "\n" |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
74 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
75 struct += "\n}" |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
76 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
77 return struct |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
78 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
79 # Prints out what the image will look like on the display |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
80 def preview( self ): |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
81 # Top border first |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
82 display = "+" |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
83 for pixel in range( 0, self.width ): |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
84 display += "-" |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
85 display += "+\n" |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
86 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
87 # Each Page |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
88 for page in range( self.page_count - 1, -1, -1 ): |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
89 # Each Bit (Line) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
90 for line in range( 7, -1, -1 ): |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
91 # Border |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
92 display += "|" |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
93 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
94 # Each Byte (Column/Pixel) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
95 for byte in range( 0, self.width ): |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
96 if self.page_data[ page ][ byte ] & (1 << line): |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
97 display += "*" |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
98 else: |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
99 display += " " |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
100 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
101 # Border |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
102 display += "|\n" |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
103 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
104 # Bottom border |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
105 display += "+" |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
106 for pixel in range( 0, self.width ): |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
107 display += "-" |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
108 display += "+\n" |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
109 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
110 return display |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
111 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
112 |
398
63b1d003fd50
Stop requiring editing of example scripts
Joshua Flanagan <joshuaflanagan@gmail.com>
parents:
332
diff
changeset
|
113 filename = sys.argv[1] |
63b1d003fd50
Stop requiring editing of example scripts
Joshua Flanagan <joshuaflanagan@gmail.com>
parents:
332
diff
changeset
|
114 if filename is None: |
63b1d003fd50
Stop requiring editing of example scripts
Joshua Flanagan <joshuaflanagan@gmail.com>
parents:
332
diff
changeset
|
115 print( "You must specify a bitmap filename. Try './bitmap2Struct.py ic_logo_lcd.bmp'" ) |
63b1d003fd50
Stop requiring editing of example scripts
Joshua Flanagan <joshuaflanagan@gmail.com>
parents:
332
diff
changeset
|
116 sys.exit( 1 ) |
332
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
117 max_height = 32 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
118 max_width = 128 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
119 x_offset = 0 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
120 y_offset = 0 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
121 output_image = STLcdGraphic( max_height, max_width ) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
122 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
123 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
124 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
125 # Load the input filename and convert to black & white |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
126 try: |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
127 input_image = Image.open( filename ).convert('1') |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
128 except: |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
129 print( "Unable to load image '{0}'".format( filename ) ) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
130 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
131 # Check the image size to see if within the bounds of the display |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
132 if input_image.size[0] > max_width or input_image.size[1] > max_height: |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
133 print( "ERROR: '{0}:{1}' is too large, must be no larger than {2}x{3}".format( |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
134 filename, |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
135 ( input_image.format, input_image.size, input_image.mode ), |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
136 max_width, |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
137 max_height ) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
138 ) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
139 sys.exit( 1 ) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
140 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
141 # Center the image |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
142 height_start = int( ( max_height - input_image.size[1] ) / 2 ) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
143 height_end = int( height_start + input_image.size[1] ) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
144 width_start = int( ( max_width - input_image.size[0] ) / 2 ) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
145 width_end = int( width_start + input_image.size[0] ) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
146 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
147 #print( height_start ) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
148 #print( height_end ) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
149 #print( width_start ) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
150 #print( width_end ) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
151 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
152 # Iterate over all of the pixels |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
153 # Also prepare the debug view of the image (disp_test) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
154 disp_test = "+" |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
155 for pixel in range( 0, max_width ): |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
156 disp_test += "-" |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
157 disp_test += "+\n" |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
158 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
159 for y in range( 0, max_height ): |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
160 disp_test += "|" |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
161 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
162 # Check if within height range |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
163 if not ( y >= height_start and y < height_end ): |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
164 disp_test += " " * max_width + "|\n|" |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
165 continue |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
166 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
167 for x in range( 0, max_width ): |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
168 # Check if within width range |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
169 if not ( x >= width_start and x < width_end ): |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
170 disp_test += " " |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
171 continue |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
172 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
173 # Use image value to determine pixel |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
174 try: |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
175 if input_image.getpixel( (x - width_start, y - height_start) ) == 0: |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
176 disp_test += "*" |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
177 output_image.setpixel( x, y + 1 ) # +1 is due to page boundary |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
178 else: |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
179 disp_test += " " |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
180 except IndexError: |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
181 print( (x - width_start,y - height_start) ) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
182 pass |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
183 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
184 disp_test += "|\n" |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
185 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
186 disp_test += "+" |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
187 for pixel in range( 0, max_width ): |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
188 disp_test += "-" |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
189 disp_test += "+\n" |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
190 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
191 # BMP Conversion preview |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
192 print( disp_test ) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
193 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
194 |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
195 print ( output_image.preview() ) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
196 #print () |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
197 print( "uint8_t array[] = {0};".format( output_image.getarray() ) ) |
2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
Jacob Alexander <haata@kiibohd.com>
parents:
diff
changeset
|
198 |