Created
August 4, 2020 16:34
-
-
Save cowboy/4a56a2b95756e2663386e0b170191223 to your computer and use it in GitHub Desktop.
animated gif viewer for UnicornHD
This file contains hidden or 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 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