Skip to content

Commit d7d88d8

Browse files
committed
Allow image drawing in subscreen areas
This extends the `image` method with two additional arguments for the image origin on the screen. The previous exact size check was adapted to ensure that the image drawn at the supplied origin does not exceed the display dimensions. The changes allow to update a smaller part of the screen with an image which is especially benefitial on lower end hardware. Some example test results from a Rasberry Pi Zero: - Draw fullscreen image (240x320): ~0.3s - Draw half-screen image (240x160): ~0.2s - Draw single line of text (240x22): ~0.06s Fixes: #48
1 parent 4b496c2 commit d7d88d8

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

adafruit_rgb_display/rgb.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,10 @@ def pixel(self, x, y, color=None):
175175
self._block(x, y, x, y, self._encode_pixel(color))
176176
return None
177177

178-
def image(self, img, rotation=None):
179-
"""Set buffer to value of Python Imaging Library image. The image should
180-
be in 1 bit mode and a size equal to the display size."""
178+
def image(self, img, rotation=None, x=0, y=0):
179+
"""Set buffer to value of Python Imaging Library image. The image should
180+
be in 1 bit mode and a size not exceeding the display size when drawn at
181+
the supplied origin."""
181182
if rotation is None:
182183
rotation = self.rotation
183184
if not img.mode in ('RGB', 'RGBA'):
@@ -187,20 +188,20 @@ def image(self, img, rotation=None):
187188
if rotation != 0:
188189
img = img.rotate(rotation, expand=True)
189190
imwidth, imheight = img.size
190-
if imwidth != self.width or imheight != self.height:
191-
raise ValueError('Image must be same dimensions as display ({0}x{1}).' \
191+
if x + imwidth > self.width or y + imheight > self.height:
192+
raise ValueError('Image must not exceed dimensions of display ({0}x{1}).' \
192193
.format(self.width, self.height))
193194
if numpy:
194195
pixels = list(image_to_data(img))
195196
else:
196197
# Slower but doesn't require numpy
197-
pixels = bytearray(self.width * self.height * 2)
198-
for x in range(self.width):
199-
for y in range(self.height):
200-
pix = color565(img.getpixel((x, y)))
201-
pixels[2*(y * self.width + x)] = pix >> 8
202-
pixels[2*(y * self.width + x) + 1] = pix & 0xFF
203-
self._block(0, 0, self.width - 1, self.height - 1, pixels)
198+
pixels = bytearray(imwidth * imheight * 2)
199+
for i in range(imwidth):
200+
for j in range(imheight):
201+
pix = color565(img.getpixel((i, j)))
202+
pixels[2*(j * imwidth + i)] = pix >> 8
203+
pixels[2*(j * imwidth + i) + 1] = pix & 0xFF
204+
self._block(x, y, x + imwidth - 1, y + imheight - 1, pixels)
204205

205206
#pylint: disable-msg=too-many-arguments
206207
def fill_rectangle(self, x, y, width, height, color):

0 commit comments

Comments
 (0)