-
Notifications
You must be signed in to change notification settings - Fork 6
/
display_gif.py
63 lines (49 loc) · 1.93 KB
/
display_gif.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""Displays an animated gif on a 32x32 RGB LED Adafruit Matrix."""
import argparse
from pathlib import Path
import time
from PIL import Image, ImageSequence
from rgbmatrix import RGBMatrix, RGBMatrixOptions
def get_frames(path):
"""Returns an iterable of gif frames."""
frames = []
with Image.open(path) as gif:
for frame in ImageSequence.Iterator(gif):
frame = frame.convert('RGB').resize((32, 32))
frames.append(frame)
return frames
def display_gif(path):
"""Displays gif frames on matrix."""
options = RGBMatrixOptions()
options.rows = 32
options.chain_length = 1
options.parallel = 1
options.hardware_mapping = 'adafruit-hat'
matrix = RGBMatrix(options=options)
while True:
for frame in get_frames(path):
matrix.SetImage(frame)
time.sleep(frame.info['duration']/1000)
def _get_parser():
"""Parses command line arguments."""
parser = argparse.ArgumentParser(description='Display gif on LED matrix.')
parser.add_argument('gif_path',
nargs='?',
metavar='PATH_TO_GIF',
type=Path,
default=Path(__file__).parent / 'myGIF.gif',
help='the path to the gif file')
return parser
if __name__ == '__main__':
print('*********************************************\n'
'display_gif script created by Heather Mahan.\n'
'For more information, see documentation at\n'
'https://github.com/poemusica/rpi-matrix-gif\n'
'*********************************************')
parser = _get_parser()
args = parser.parse_args()
parser.print_help()
print('*********************************************'
'\nThis script uses the rgbmatrix library by hzeller:\n'
'https://github.com/hzeller/rpi-rgb-led-matrix')
display_gif(args.gif_path)