Last active
February 8, 2024 10:20
-
-
Save Simon-Ince/910c440eb34bb722afa90853ecfb1d85 to your computer and use it in GitHub Desktop.
Show gifs on and LED matrix using the hzeller/rpi-rgb-led-matrix Python library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import time | |
import sys | |
from rgbmatrix import RGBMatrix, RGBMatrixOptions | |
from PIL import Image, ImageSequence, GifImagePlugin | |
# Matrix size | |
size = 64, 64 | |
# Open source | |
if len(sys.argv) < 2: | |
sys.exit("Require an image argument") | |
else : | |
image_file = sys.argv[1] | |
im = Image.open(image_file) | |
# Get gif frames | |
frames = ImageSequence.Iterator(im) | |
# Resize gif frames to matrix | |
def thumbnails(frames): | |
for frame in frames: | |
thumbnail = frame.copy() | |
thumbnail.thumbnail(size, Image.ANTIALIAS) | |
yield thumbnail | |
frames = thumbnails(frames) | |
# Save output | |
om = next(frames)# Handle first frame separately | |
om.info = im.info# Copy sequence info | |
om.save("out.gif", save_all = True, append_images = list(frames)) | |
# Pull back in resized gif | |
image = Image.open("out.gif") | |
# Configuration matrix for the matrix | |
options = RGBMatrixOptions() | |
options.rows = 64 | |
options.cols = 64 | |
options.chain_length = 1 | |
options.parallel = 1 | |
options.hardware_mapping = 'adafruit-hat' | |
matrix = RGBMatrix(options = options) | |
# Loop through gif frames and display on matrix. | |
while True: | |
for frame in range(0, image.n_frames): | |
print(image.n_frames) | |
image.seek(frame) | |
matrix.SetImage(image.convert('RGB')) | |
time.sleep(1) | |
# Handle quiting | |
try: | |
print("Press CTRL-C to stop.") | |
while True: | |
time.sleep(100) | |
except KeyboardInterrupt: | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello
I copied the code and got an error on line 16 "Invalid non-printable character U+00A0. Do you know how to fix this?