Skip to content

Instantly share code, notes, and snippets.

@Simon-Ince
Last active February 8, 2024 10:20
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Simon-Ince/910c440eb34bb722afa90853ecfb1d85 to your computer and use it in GitHub Desktop.
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
#!/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)
@KillahB33
Copy link

I know this is old, but just wanted to say thanks!

@codewerker
Copy link

THANKS!!!

@kylejohnsonkj
Copy link

Thanks! I got weird artifacting when I loaded my gif without resizing the frames this way first.

@Gitpnda
Copy link

Gitpnda commented Oct 25, 2022

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment