From 3f6aab6c9bd7e2a5276cb015a0328c97389b44eb Mon Sep 17 00:00:00 2001 From: Pat Satyshur Date: Sun, 20 Feb 2022 19:27:20 -0600 Subject: [PATCH 1/2] Added check for numpy, and overloaded the image function to uses numpy if available. --- adafruit_sharpmemorydisplay.py | 43 ++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/adafruit_sharpmemorydisplay.py b/adafruit_sharpmemorydisplay.py index 34804c0..398fd20 100644 --- a/adafruit_sharpmemorydisplay.py +++ b/adafruit_sharpmemorydisplay.py @@ -31,6 +31,11 @@ from micropython import const import adafruit_framebuf +try: + import numpy +except ImportError: + numpy = None + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SharpMemoryDisplay.git" @@ -103,3 +108,41 @@ def show(self): self._spi.write(self._buf) # we send one last 0 byte self._scs_pin.value = False self._spi.unlock() + + 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.""" + # determine our effective width/height, taking rotation into account + width = self.width + height = self.height + if self.rotation in (1, 3): + width, height = height, width + + if img.mode != "1": + raise ValueError("Image must be in mode 1.") + + imwidth, imheight = img.size + if imwidth != width or imheight != height: + raise ValueError( + "Image must be same dimensions as display ({0}x{1}).".format( + width, height + ) + ) + + if numpy: + self.buffer = bytearray(numpy.packbits(numpy.asarray(img), axis=1).flatten().tolist()) + else: + # Grab all the pixels from the image, faster than getpixel. + pixels = img.load() + # Clear buffer + for i in range(len(self.buf)): # pylint: disable=consider-using-enumerate + self.buf[i] = 0 + # Iterate through the pixels + for x in range(width): # yes this double loop is slow, + for y in range(height): # but these displays are small! + if img.mode == "RGB": + self.pixel(x, y, pixels[(x, y)]) + elif pixels[(x, y)]: + self.pixel(x, y, 1) # only write if pixel is true + + From f96d82bcf1212a6178f67527e44dc0de5bb69b45 Mon Sep 17 00:00:00 2001 From: Pat Satyshur Date: Sun, 20 Feb 2022 22:13:24 -0600 Subject: [PATCH 2/2] Update formatting to make black happy --- adafruit_sharpmemorydisplay.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/adafruit_sharpmemorydisplay.py b/adafruit_sharpmemorydisplay.py index 398fd20..4c2a69f 100644 --- a/adafruit_sharpmemorydisplay.py +++ b/adafruit_sharpmemorydisplay.py @@ -128,9 +128,11 @@ def image(self, img): width, height ) ) - + if numpy: - self.buffer = bytearray(numpy.packbits(numpy.asarray(img), axis=1).flatten().tolist()) + self.buffer = bytearray( + numpy.packbits(numpy.asarray(img), axis=1).flatten().tolist() + ) else: # Grab all the pixels from the image, faster than getpixel. pixels = img.load() @@ -144,5 +146,3 @@ def image(self, img): self.pixel(x, y, pixels[(x, y)]) elif pixels[(x, y)]: self.pixel(x, y, 1) # only write if pixel is true - -