Skip to content

Commit d0e9028

Browse files
authored
Merge pull request #8 from ladyada/master
add image()
2 parents 6aec69f + e4901cb commit d0e9028

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

adafruit_framebuf.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,26 @@ def text(self, string, x, y, color, *,
268268
x + (i * (w + 1)),
269269
y, self, color)
270270

271+
def image(self, img):
272+
"""Set buffer to value of Python Imaging Library image. The image should
273+
be in 1 bit mode and a size equal to the display size."""
274+
if img.mode != '1':
275+
raise ValueError('Image must be in mode 1.')
276+
imwidth, imheight = img.size
277+
if imwidth != self.width or imheight != self.height:
278+
raise ValueError('Image must be same dimensions as display ({0}x{1}).' \
279+
.format(self.width, self.height))
280+
# Grab all the pixels from the image, faster than getpixel.
281+
pixels = img.load()
282+
# Clear buffer
283+
for i in range(len(self.buf)):
284+
self.buf[i] = 0
285+
# Iterate through the pixels
286+
for x in range(self.width): # yes this double loop is slow,
287+
for y in range(self.height): # but these displays are small!
288+
if pixels[(x, y)]:
289+
self.pixel(x, y, 1) # only write if pixel is true
290+
271291
# MicroPython basic bitmap font renderer.
272292
# Author: Tony DiCola
273293
# License: MIT License (https://opensource.org/licenses/MIT)

0 commit comments

Comments
 (0)