Skip to content

Instantly share code, notes, and snippets.

@retrage
Last active January 6, 2016 12:13
Show Gist options
  • Save retrage/0f290b693b15fc0b5a19 to your computer and use it in GitHub Desktop.
Save retrage/0f290b693b15fc0b5a19 to your computer and use it in GitHub Desktop.
This script converts an image to arduino functions using u8glib.
#!/usr/bin/python
# -*- coding: utf-8 -*-
from PIL import Image
import sys
funccall_tmp = "u8g.drawBitmapP({0}, {1}, 1, 8, {2});"
def draw(px, width, height):
funcs = list()
for y in range(int(height/8) if height % 8 == 0 else int(height/8+1)):
for x in range(int(width/8) if width % 8 == 0 else int(width/8+1)):
func = "bitmap_{0}x{1}".format(str(x*8), str(y*8))
funccall = funccall_tmp.format(str(x*8), str(y*8), func)
funcs.append(funccall)
print("const uint8_t "+func+"[] PROGMEM = {")
for j in range(8):
s = str()
for i in range(8):
try:
if px[x*8+i, y*8+j] == 0:
s += '1'
else:
s += '0'
except IndexError:
s += '0'
print("\t"+hex(int(s, 2))+",")
print("};");
print("void draw(void) {")
for f in funcs:
print("\t"+f)
print("}")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: "+sys.argv[0]+" FileName")
sys.exit()
target_size = (128, 64)
try:
with Image.open(sys.argv[1]) as im:
resizedIm = im.resize(target_size)
convertedIm = resizedIm.convert('1')
convertedIm.show()
draw(convertedIm.load(), target_size[0], target_size[1])
except IOError:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment