Skip to content

Instantly share code, notes, and snippets.

@todbot
Last active August 3, 2022 05:15
Show Gist options
  • Save todbot/1bc6936f273a06667b0e44de5d10cc46 to your computer and use it in GitHub Desktop.
Save todbot/1bc6936f273a06667b0e44de5d10cc46 to your computer and use it in GitHub Desktop.
play a BMP list like an animated gif in CircuitPython
# 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!")
@degan6
Copy link

degan6 commented Aug 3, 2022

Thank you so much for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment