Last active
August 3, 2022 05:15
-
-
Save todbot/1bc6936f273a06667b0e44de5d10cc46 to your computer and use it in GitHub Desktop.
play a BMP list like an animated gif in CircuitPython
This file contains 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
# animated_gif_player.py - play a BMP list like an animated gif | |
# Take an animated gif, convert it to a list of numbered BMP files. | |
# For instance, with ImageMagick you can do this with: | |
# convert anim.gif -coalesce -resize 120x120 BMP:anim-%03d.bmp | |
# This creates a set of 120x120 BMP named "anim-000.bmp", "anim-001.bmp" | |
# Copy all these files to your "ani_dir" as set below, say with: | |
# cp anim*bmp /Volumes/CIRCUITPY/ani | |
# 17 Jul 2021 - @todbot | |
import time, board | |
import os | |
import displayio | |
# directory on CIRCUITPY where BMP animation frames are stored | |
ani_dir="/ani" | |
ani_files = os.listdir( ani_dir ) | |
display = board.DISPLAY # works on PyGamer, FunHouse, etc. | |
screen = displayio.Group() | |
display.show(screen) | |
while True: | |
for name in ani_files: | |
filename = ani_dir + "/" + name | |
print("playing ",filename) | |
with open(filename, "rb") as f: | |
odb = displayio.OnDiskBitmap(f) | |
bitmap = displayio.TileGrid(odb, pixel_shader=odb.pixel_shader) | |
screen.append(bitmap) | |
# Wait for the image to load. | |
display.refresh(target_frames_per_second=20) | |
screen.pop() # remove bitmap so we don't run out of memory | |
print("at end, going again!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much for this!