3 # Copyright (C) 2015 by Jacob Alexander
5 # This file 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 file 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 file. If not, see <http://www.gnu.org/licenses/>.
22 from PIL import Image # Use pillow instead of PIL, it works with Python 3
25 # Convenience class to deal with converting images to a C array
27 # Some constants for the LCD Driver
33 def __init__( self, height, width ):
37 # Calculate number of pages
38 self.page_count = int( self.height / self.page_width )
39 self.page_length = self.width
41 # Initialize pages to 0's
43 for page in range( 0, self.page_count ):
44 self.page_data.append( array( 'B', [0] * self.page_length ) )
46 def setpixel( self, x, y ):
47 # Calculate which page
48 page = int( ( self.height - y ) / self.page_width )
53 # Calculate which byte
57 bit = int( ( self.height - y ) % self.page_width )
60 self.page_data[ page ][ byte ] |= (1 << bit)
62 def getpage( self, page ):
63 return self.page_data[ page ]
68 for page in range( 0, self.page_count ):
69 for elem in self.page_data[ page ]:
70 struct += "0x{0:02x}, ".format( elem )
72 if page != self.page_count - 1:
79 # Prints out what the image will look like on the display
83 for pixel in range( 0, self.width ):
88 for page in range( self.page_count - 1, -1, -1 ):
90 for line in range( 7, -1, -1 ):
94 # Each Byte (Column/Pixel)
95 for byte in range( 0, self.width ):
96 if self.page_data[ page ][ byte ] & (1 << line):
106 for pixel in range( 0, self.width ):
113 filename = sys.argv[1]
115 print( "You must specify a bitmap filename. Try './bitmap2Struct.py ic_logo_lcd.bmp'" )
121 output_image = STLcdGraphic( max_height, max_width )
125 # Load the input filename and convert to black & white
127 input_image = Image.open( filename ).convert('1')
129 print( "Unable to load image '{0}'".format( filename ) )
131 # Check the image size to see if within the bounds of the display
132 if input_image.size[0] > max_width or input_image.size[1] > max_height:
133 print( "ERROR: '{0}:{1}' is too large, must be no larger than {2}x{3}".format(
135 ( input_image.format, input_image.size, input_image.mode ),
142 height_start = int( ( max_height - input_image.size[1] ) / 2 )
143 height_end = int( height_start + input_image.size[1] )
144 width_start = int( ( max_width - input_image.size[0] ) / 2 )
145 width_end = int( width_start + input_image.size[0] )
147 #print( height_start )
149 #print( width_start )
152 # Iterate over all of the pixels
153 # Also prepare the debug view of the image (disp_test)
155 for pixel in range( 0, max_width ):
159 for y in range( 0, max_height ):
162 # Check if within height range
163 if not ( y >= height_start and y < height_end ):
164 disp_test += " " * max_width + "|\n|"
167 for x in range( 0, max_width ):
168 # Check if within width range
169 if not ( x >= width_start and x < width_end ):
173 # Use image value to determine pixel
175 if input_image.getpixel( (x - width_start, y - height_start) ) == 0:
177 output_image.setpixel( x, y + 1 ) # +1 is due to page boundary
181 print( (x - width_start,y - height_start) )
187 for pixel in range( 0, max_width ):
191 # BMP Conversion preview
195 print ( output_image.preview() )
197 print( "uint8_t array[] = {0};".format( output_image.getarray() ) )