Skip to content

Instantly share code, notes, and snippets.

@christianromney
Created March 16, 2016 15:52
Show Gist options
  • Save christianromney/176be48902a89e058524 to your computer and use it in GitHub Desktop.
Save christianromney/176be48902a89e058524 to your computer and use it in GitHub Desktop.
# (c) 2016 Mario Cruz
# please credit me if you modify and/or use this code
# not for commercial use
# lots of help from Giles Booth @blogmywiki
from random import *
from printer import *
from PIL import Image
import os
import glob
import json
def random_poem():
path = os.path.join(os.getcwd(), "poems/*.json")
list = glob.glob(path)
if (0 < len(list)):
idx = random.randrange(0, len(list))
with open(list[idx]) as f:
return json.loads(f.read())
else:
return None
def print_poem(p, poem):
p.bold()
p.inverse()
p.print_text(poem['title'])
p.linefeed()
p.bold(False)
p.inverse(False)
p.justify("L")
p.print_text("\n".join(poem['text']))
p.linefeed()
p.justify("R")
p.print_text(poem['author'])
p.justify("L")
p.linefeed()
i = Image.open("o.png")
data = list(i.getdata())
w, h = i.size
p.print_bitmap(data, w, h, True)
p.linefeed()
#FONT B IS THE TINY FONT
p.font_b()
p.print_text("O, Miami, Random Poems (c) 2016 @mariocruz Code 4 Miami")
p.font_b(False)
p.linefeed()
p.linefeed()
p.linefeed()
# Run the program
if __name__ == "__main__":
p = printer.ThermalPrinter(serialport="/dev/ttyAMA0")
poem = random_poem()
if (poem is not None):
print_poem(p, poem)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment