From 4d42aa7fc4b669cdc3cb9467c82d2416591b6f74 Mon Sep 17 00:00:00 2001 From: ladyada Date: Thu, 27 Dec 2018 21:59:52 -0500 Subject: [PATCH 1/3] optimize image() so it doesn't take foreeeever --- adafruit_framebuf.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/adafruit_framebuf.py b/adafruit_framebuf.py index 54a6e58..4dd1ccd 100644 --- a/adafruit_framebuf.py +++ b/adafruit_framebuf.py @@ -268,6 +268,27 @@ def text(self, string, x, y, color, *, x + (i * (w + 1)), y, self, color) + def image(self, img): + """Set buffer to value of Python Imaging Library image. The image should + be in 1 bit mode and a size equal to the display size.""" + if img.mode != '1': + raise ValueError('Image must be in mode 1.') + imwidth, imheight = img.size + if imwidth != self.width or imheight != self.height: + raise ValueError('Image must be same dimensions as display ({0}x{1}).' \ + .format(self.width, self.height)) + # Grab all the pixels from the image, faster than getpixel. + pixels = img.load() + # Clear buffer + for i in range(len(self.buf)): + self.buf[i] = 0 + # Iterate through the memory pages + index = 0 + for x in range(self.width): # yes this double loop is slow, + for y in range(self.height): # but these displays are small! + if pixels[(x,y)]: + self.pixel(x, y, 1) # only write if pixel is true + # MicroPython basic bitmap font renderer. # Author: Tony DiCola # License: MIT License (https://opensource.org/licenses/MIT) From 90dc8d199b40788a495211bf0791370c8674d57d Mon Sep 17 00:00:00 2001 From: ladyada Date: Thu, 27 Dec 2018 22:08:52 -0500 Subject: [PATCH 2/3] fix lint --- adafruit_framebuf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_framebuf.py b/adafruit_framebuf.py index 4dd1ccd..f46058d 100644 --- a/adafruit_framebuf.py +++ b/adafruit_framebuf.py @@ -286,7 +286,7 @@ def image(self, img): index = 0 for x in range(self.width): # yes this double loop is slow, for y in range(self.height): # but these displays are small! - if pixels[(x,y)]: + if pixels[(x, y)]: self.pixel(x, y, 1) # only write if pixel is true # MicroPython basic bitmap font renderer. From e4901cb4369f33a3a6a8f5ef3717b3f70a31b721 Mon Sep 17 00:00:00 2001 From: ladyada Date: Thu, 27 Dec 2018 22:27:06 -0500 Subject: [PATCH 3/3] lint and fix comment --- adafruit_framebuf.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/adafruit_framebuf.py b/adafruit_framebuf.py index f46058d..fa28224 100644 --- a/adafruit_framebuf.py +++ b/adafruit_framebuf.py @@ -282,8 +282,7 @@ def image(self, img): # Clear buffer for i in range(len(self.buf)): self.buf[i] = 0 - # Iterate through the memory pages - index = 0 + # Iterate through the pixels for x in range(self.width): # yes this double loop is slow, for y in range(self.height): # but these displays are small! if pixels[(x, y)]: