@@ -123,6 +123,22 @@ def shift_down(self, rotate=False):
123123 """
124124 self .shift (0 , - 1 , rotate )
125125
126+ def image (self , img ):
127+ """Set buffer to value of Python Imaging Library image. The image should
128+ be in 1 bit mode and a size equal to the display size."""
129+ imwidth , imheight = img .size
130+ if imwidth != self .columns or imheight != self .rows :
131+ raise ValueError ('Image must be same dimensions as display ({0}x{1}).' \
132+ .format (self .columns , self .rows ))
133+ # Grab all the pixels from the image, faster than getpixel.
134+ pixels = img .convert ('1' ).load ()
135+ # Iterate through the pixels
136+ for x in range (self .columns ): # yes this double loop is slow,
137+ for y in range (self .rows ): # but these displays are small!
138+ self .pixel (x , y , pixels [(x , y )])
139+ if self ._auto_write :
140+ self .show ()
141+
126142 @property
127143 def columns (self ):
128144 """Read-only property for number of columns"""
@@ -160,15 +176,21 @@ def pixel(self, x, y, color=None):
160176
161177class Matrix8x8x2 (Matrix8x8 ):
162178 """A bi-color matrix."""
179+
180+ LED_OFF = 0
181+ LED_RED = 1
182+ LED_GREEN = 2
183+ LED_YELLOW = 3
184+
163185 def pixel (self , x , y , color = None ):
164186 """Get or set the color of a given pixel."""
165187 if not 0 <= x <= 7 :
166188 return None
167189 if not 0 <= y <= 7 :
168190 return None
169191 if color is not None :
170- super ()._pixel (y , x , (color & 0x01 ) )
171- super ()._pixel (y + 8 , x , (color >> 1 ) & 0x01 )
192+ super ()._pixel (y , x , (color >> 1 ) & 0x01 )
193+ super ()._pixel (y + 8 , x , (color & 0x01 ) )
172194 else :
173195 return super ()._pixel (y , x ) | super ()._pixel (y + 8 , x ) << 1
174196 return None
@@ -182,3 +204,27 @@ def fill(self, color):
182204 self ._set_buffer (i * 2 + 1 , fill2 )
183205 if self ._auto_write :
184206 self .show ()
207+
208+ def image (self , img ):
209+ """Set buffer to value of Python Imaging Library image. The image should
210+ be a size equal to the display size."""
211+ imwidth , imheight = img .size
212+ if imwidth != self .columns or imheight != self .rows :
213+ raise ValueError ('Image must be same dimensions as display ({0}x{1}).' \
214+ .format (self .columns , self .rows ))
215+ # Grab all the pixels from the image, faster than getpixel.
216+ pixels = img .convert ('RGB' ).load ()
217+ # Iterate through the pixels
218+ for x in range (self .columns ): # yes this double loop is slow,
219+ for y in range (self .rows ): # but these displays are small!
220+ if pixels [(x , y )] == (255 , 0 , 0 ):
221+ self .pixel (x , y , self .LED_RED )
222+ elif pixels [(x , y )] == (0 , 255 , 0 ):
223+ self .pixel (x , y , self .LED_GREEN )
224+ elif pixels [(x , y )] == (255 , 255 , 0 ):
225+ self .pixel (x , y , self .LED_YELLOW )
226+ else :
227+ # Unknown color, default to LED off.
228+ self .pixel (x , y , self .LED_OFF )
229+ if self ._auto_write :
230+ self .show ()
0 commit comments