Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created August 4, 2020 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowboy/4a56a2b95756e2663386e0b170191223 to your computer and use it in GitHub Desktop.
Save cowboy/4a56a2b95756e2663386e0b170191223 to your computer and use it in GitHub Desktop.
animated gif viewer for UnicornHD
#!/usr/bin/env python3
'''
See
https://github.com/HimbeersaftLP/unicorn-hat-hd-gif-displayer/blob/master/unicorn-gif.py
'''
import signal
import time
from sys import exit
try:
from PIL import Image, ImageSequence
except ImportError:
exit("This script requires the pillow module\nInstall with: sudo pip install pillow")
try:
import unicornhathd as unicorn
print("unicorn hat hd detected")
except ImportError:
from unicorn_hat_sim import unicornhathd as unicorn
unicorn.rotation(0)
unicorn.brightness(0.2)
width, height = unicorn.get_shape()
print("Loading image...")
image = Image.open("image.gif")
print("Reading and processing frames...")
frames = [frame.copy().convert("RGBA")
for frame in ImageSequence.Iterator(image)]
print("Playing animation...\nPress Ctrl+C to stop.")
def draw_frame(frame):
valid = False
for x in range(width):
for y in range(height):
pixel = frame.getpixel((y, x))
r, g, b = int(pixel[0]), int(pixel[1]), int(pixel[2])
if r or g or b:
valid = True
unicorn.set_pixel(x, y, r, g, b)
return valid
def draw_frames():
for frame in frames:
valid = draw_frame(frame)
if valid:
unicorn.show()
time.sleep((frame.info["duration"] / 1000) - 0.02)
try:
while True:
draw_frames()
except KeyboardInterrupt:
unicorn.off()
print("\nStopped.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment