|
| 1 | +""" |
| 2 | +Example to extract the frames and other parameters from an animated gif |
| 3 | +and then run the animation on the display. |
| 4 | +
|
| 5 | +Usage: |
| 6 | +python3 is31fl3731_pillow_animated_gif.py animated.gif |
| 7 | +
|
| 8 | +This example is for use on (Linux) computers that are using CPython with |
| 9 | +Adafruit Blinka to support CircuitPython libraries. CircuitPython does |
| 10 | +not support PIL/pillow (python imaging library)! |
| 11 | +
|
| 12 | +Author(s): Melissa LeBlanc-Williams for Adafruit Industries |
| 13 | +""" |
| 14 | + |
| 15 | +import sys |
| 16 | +import board |
| 17 | +from PIL import Image |
| 18 | +import adafruit_is31fl3731 |
| 19 | + |
| 20 | +i2c = board.I2C() |
| 21 | + |
| 22 | +# uncomment line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix |
| 23 | +#display = adafruit_is31fl3731.Matrix(i2c) |
| 24 | +# uncomment line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix |
| 25 | +display = adafruit_is31fl3731.CharlieBonnet(i2c) |
| 26 | +display.fill(0) |
| 27 | + |
| 28 | +# Open the gif |
| 29 | +if len(sys.argv) < 2: |
| 30 | + print("No image file specified") |
| 31 | + print("Usage: python3 is31fl3731_pillow_animated_gif.py animated.gif") |
| 32 | + sys.exit() |
| 33 | + |
| 34 | +image = Image.open(sys.argv[1]) |
| 35 | + |
| 36 | +# Make sure it's animated |
| 37 | +if not image.is_animated: |
| 38 | + print("Specified image is not animated") |
| 39 | + sys.exit() |
| 40 | + |
| 41 | +# Get the autoplay information from the gif |
| 42 | +delay = image.info['duration'] |
| 43 | +loops = image.info['loop'] |
| 44 | + |
| 45 | +# Adjust loop count (0 is forever) |
| 46 | +if loops > 0: |
| 47 | + loops += 1 |
| 48 | + |
| 49 | +# IS31FL3731 only supports 0-7 |
| 50 | +if loops > 7: |
| 51 | + loops = 7 |
| 52 | + |
| 53 | +# Get the frame count (maximum 8 frames) |
| 54 | +frame_count = image.n_frames |
| 55 | +if frame_count > 8: |
| 56 | + frame_count = 8 |
| 57 | + |
| 58 | +# Load each frame of the gif onto the Matrix |
| 59 | +for frame in range(frame_count): |
| 60 | + image.seek(frame) |
| 61 | + frame_image = Image.new('L', (display.width, display.height)) |
| 62 | + frame_image.paste(image.convert("L"), (display.width // 2 - image.width // 2, |
| 63 | + display.height // 2 - image.height // 2)) |
| 64 | + display.image(frame_image, frame=frame) |
| 65 | + |
| 66 | +display.autoplay(delay=delay, loops=loops) |
0 commit comments