Skip to content

Instantly share code, notes, and snippets.

@isjerryxiao
Last active September 28, 2017 13:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isjerryxiao/982774a7bf0622631a1faa02b3567168 to your computer and use it in GitHub Desktop.
Save isjerryxiao/982774a7bf0622631a1faa02b3567168 to your computer and use it in GitHub Desktop.
Play a series of converted images on a single max7219 8x8 led matrix. https://github.com/rm-hull/luma.led_matrix
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os, sys, time, threading
from luma.core.interface.serial import spi, noop
from luma.led_matrix.device import max7219
from PIL import Image
interval=1/30
serial = spi(port=0, device=0, gpio=noop())
device = max7219(serial, rotate=3)
device.contrast(10)
lock = threading.Lock()
def analyze(current, liststart=0):
realtime=(time.time() - starttick)
playertime=(liststart + current)*interval
if realtime > playertime and (realtime - playertime) > interval:
return 0
else:
return (playertime - realtime + interval)
def showPic(imgfile):
lock.acquire()
img = Image.open(os.path.join("images/", imgfile))
device.display(img)
lock.release()
def convertShowPic(imgfile):
lock.acquire()
img = Image.open(os.path.join("images/", imgfile))
img=img.convert(mode="1")
img=img.resize((8, 8), resample=Image.LANCZOS)
device.display(img)
lock.release()
try:
print("loading...")
imglist=sorted(os.listdir("images/"), key=lambda x :int(x[:-4]))
print("start!")
starttick=time.time()
args=sys.argv
if len(args) > 1:
if args[1] == "convert":
print("convert mode.")
for imgfile in imglist:
threading.Thread(target=convertShowPic(imgfile), args=(), name='thread-'+imgfile).start()
time.sleep(analyze(int(imgfile[:-4])))
else:
print("rtfs please.")
else:
for imgfile in imglist:
threading.Thread(target=showPic(imgfile), args=(), name='thread-'+imgfile).start()
time.sleep(analyze(int(imgfile[:-4])))
print("Ended.")
device.cleanup()
except (KeyboardInterrupt, SystemExit):
device.cleanup()
print("Cleaning up...")
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment