As always, you need to activate the virtual environment before running the example. If you haven't already then activate it like this.
source ~/venvs/blinka_venv/bin/activate
Next you can run the quote_scroller.py script.
python quote_scroller.py
The code for this example is embedded below. You can download it, or copy/paste the text into a file.
#!/usr/bin/python3 # SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries # # SPDX-License-Identifier: MIT """ Display quote from the Adafruit quotes API as text scrolling across the matrices. Requires the requests library to be installed. Run like this: $ python quote_scroller.py """ import numpy as np import requests from PIL import Image, ImageDraw, ImageFont import adafruit_blinka_raspberry_pi5_piomatter as piomatter # 128px for 2x1 matrices. Change to 64 if you're using a single matrix. total_width = 128 total_height = 32 bottom_half_shift_compensation = 1 font_color = (0, 128, 128) # Load the font font = ImageFont.truetype("LindenHill-webfont.ttf", 26) quote_resp = requests.get("https://www.adafruit.com/api/quotes.php").json() text = f'{quote_resp[0]["text"]} - {quote_resp[0]["author"]}' #text = "Sometimes you just want to use hardcoded strings. - Unknown" x, y, text_width, text_height = font.getbbox(text) full_txt_img = Image.new("RGB", (int(text_width) + 6, int(text_height) + 6), (0, 0, 0)) draw = ImageDraw.Draw(full_txt_img) draw.text((3, 3), text, font=font, fill=font_color) full_txt_img.save("quote.png") single_frame_img = Image.new("RGB", (total_width, total_height), (0, 0, 0)) geometry = piomatter.Geometry(width=total_width, height=total_height, n_addr_lines=4, rotation=piomatter.Orientation.Normal) framebuffer = np.asarray(single_frame_img) + 0 # Make a mutable copy matrix = piomatter.PioMatter(colorspace=piomatter.Colorspace.RGB888Packed, pinout=piomatter.Pinout.AdafruitMatrixBonnet, framebuffer=framebuffer, geometry=geometry) print("Ctrl-C to exit") while True: for x_pixel in range(-total_width-1,full_txt_img.width): if bottom_half_shift_compensation == 0: # full paste single_frame_img.paste(full_txt_img.crop((x_pixel, 0, x_pixel + total_width, total_height)), (0, 0)) else: # top half single_frame_img.paste(full_txt_img.crop((x_pixel, 0, x_pixel + total_width, total_height//2)), (0, 0)) # bottom half shift compensation single_frame_img.paste(full_txt_img.crop((x_pixel, total_height//2, x_pixel + total_width, total_height)), (bottom_half_shift_compensation, total_height//2)) framebuffer[:] = np.asarray(single_frame_img) matrix.show()
Code Explanation
This example fetches a quote from the Adafruit Quotes API using the requests
library. Once fetched the quote is rendered in it's entirety in a single line into a very wide image full_txt_img
. This image is too wide to display all at once on the matrix hence the need for scrolling.Â
The matrix gets initialized and then the code moves to the main loop. Once inside, an inner for loop iterates over horizontal pixel indexes. A matrix sized section of the full_text_img
is copied out and pasted into single_frame_img
. From there it's copied into the framebuffer
and shown on the matrix.
Page last edited February 05, 2025
Text editor powered by tinymce.