diff --git a/adafruit_is31fl3731.py b/adafruit_is31fl3731.py index 35122fb..d5e1a2e 100644 --- a/adafruit_is31fl3731.py +++ b/adafruit_is31fl3731.py @@ -27,7 +27,7 @@ CircuitPython driver for the IS31FL3731 charlieplex IC. -* Author(s): Tony DiCola +* Author(s): Tony DiCola, Melissa LeBlanc-Williams Implementation Notes -------------------- @@ -336,6 +336,28 @@ def pixel(self, x, y, color=None, blink=None, frame=None): return None #pylint: enable-msg=too-many-arguments + def image(self, img, blink=None, frame=None): + """Set buffer to value of Python Imaging Library image. The image should + be in 8-bit mode (L) and a size equal to the display size. + + :param img: Python Imaging Library image + :param blink: True to blink + :param frame: the frame to set the image + """ + if img.mode != 'L': + raise ValueError('Image must be in mode L.') + 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() + + # 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! + self.pixel(x, y, pixels[(x, y)], blink=blink, frame=frame) + class CharlieWing(Matrix): """Supports the Charlieplexed feather wing diff --git a/examples/is31fl3731_pillow_numbers.py b/examples/is31fl3731_pillow_numbers.py new file mode 100644 index 0000000..6656f26 --- /dev/null +++ b/examples/is31fl3731_pillow_numbers.py @@ -0,0 +1,34 @@ +""" +Example to utilize the Python Imaging Library (Pillow) and draw bitmapped text +to 8 frames and then run autoplay on those frames. + +Author(s): Melissa LeBlanc-Williams for Adafruit Industries +""" + +import board +from PIL import Image, ImageDraw, ImageFont +import adafruit_is31fl3731 + +i2c = board.I2C() + +# uncomment line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix +#display = adafruit_is31fl3731.Matrix(i2c) +# uncomment line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix +display = adafruit_is31fl3731.CharlieBonnet(i2c) + +display.fill(0) + +# 256 Color Grayscale Mode +image = Image.new('L', (display.width, display.height)) +draw = ImageDraw.Draw(image) + +# Load a font in 2 different sizes. +font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 10) + +# Load the text in each frame +for x in range(8): + draw.rectangle((0, 0, display.width, display.height), outline=0, fill=0) + draw.text((x + 1, -2), str(x + 1), font=font, fill=32) + display.image(image, frame=x) + +display.autoplay(delay=500)