Skip to content

Instantly share code, notes, and snippets.

@ntrrgc
Created May 27, 2015 22:59
Show Gist options
  • Save ntrrgc/57edc52f66353d266ead to your computer and use it in GitHub Desktop.
Save ntrrgc/57edc52f66353d266ead to your computer and use it in GitHub Desktop.
Digole OLED monochrome video codec (accepts animated GIF or static images)
#encoding: utf-8
import sys
import time
import PIL.Image
def marshal_image(image):
data = image.convert('1')
w, h = data.size
out = bytearray()
for y in range(h):
buf = 0
buf_len = 0
for x in range(w):
pixel = 1 if data.getpixel((x, y)) > 0 else 0
buf |= pixel << (7 - buf_len)
buf_len += 1
if buf_len == 8:
out += bytearray([buf])
buf = 0
buf_len = 0
return out
def get_images():
gif = PIL.Image.open(sys.argv[1])
yield marshal_image(gif)
try:
while 1:
gif.seek(gif.tell() + 1)
yield marshal_image(gif)
except EOFError:
pass
with open(sys.argv[2], 'wb') as f:
for noframe, imdata in enumerate(get_images()):
f.write(imdata)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment