Skip to content

Instantly share code, notes, and snippets.

@christianromney
Created March 16, 2016 16:11
Show Gist options
  • Save christianromney/ab35cfa45a5b99d7ccd7 to your computer and use it in GitHub Desktop.
Save christianromney/ab35cfa45a5b99d7ccd7 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
# and Christian Romney Twitter: @christianromney
import os
import glob
import json
import random
import printer
from PIL import Image
from StringIO import StringIO
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.load(f)
else:
return None
def poem_text(poem):
return "\n".join(poem['text'])
def console_print(poem):
print(poem['title'])
print("by " + poem['author'])
print(poem_text(poem))
def printer_print(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(poem_text(poem))
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):
console_print(poem)
printer_print(poem)
print_poem(p, poem)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment