def printstuff(wrapped_title, wrapped_author, wrapped_desc): p = printer.ThermalPrinter(serialport="/dev/ttyAMA0") p.print_text("\nHey book lovers! How's it going?\n") p.justify("C") p.print_text("Check out") p.linefeed() p.justify("C") p.underline_on() p.print_text(wrapped_title) p.underline_off() p.linefeed() p.justify("C") p.print_text("by ") p.underline_on() p.print_text(wrapped_author) p.underline_off() p.linefeed() p.justify() p.print_text(wrapped_desc) p.linefeed() p.linefeed() p.linefeed() p.linefeed() p.linefeed() return
def byte_print(byte_file_path, serialport='/dev/ttyAMA0'): '''Prints the corresponding byte file on paper. Takes: byte_path-> str (pointing to a text file) Returns: 1-> int ''' thermal = printer.ThermalPrinter(serialport=serialport) thermal.print_from_byte_file(byte_file_path) print("printing %s" % byte_file_path) return 1
def image_print(image_path, serialport='/dev/ttyAMA0'): '''Prints the corresponding image file on paper. Takes: image_path-> str (pointing to an RGB/RGBA image) Returns: 1-> int ''' thermal = printer.ThermalPrinter(serialport=serialport) img = Image.open(image_path) img = img.convert("1") #Convert to single channel data = list(img.getdata()) w, h = img.size thermal.print_bitmap(data, w, h, False) return 1
def __init__(self): printer.ThermalPrinter.BAUDRATE = 9600 self.printer = printer.ThermalPrinter(serialport='/dev/ttyUSB0') self.logf = open(LOG_FILE, 'a') self.client = mqtt.Client() self.set_key(KEY) self.state = PRINTED # Require reset self.state_time = 0 self.heartbeat_time = time.time() self.lock = threading.Lock() # GPIO setup GPIO.setmode(GPIO.BCM) GPIO.setup(RESET_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(LED_RED, GPIO.OUT) GPIO.setup(LED_GREEN, GPIO.OUT)
def main(): serialport = get_serialport() baudrate = get_baudrate() #create printer object p = printer.ThermalPrinter(serialport=serialport, baudrate=baudrate) #get our data as an array from read_in() lines = read_in() p.justify('L') # print textwrap.fill('u' + lines['content'].encode('utf-8').strip()) + '\n' p.print_text(textwrap.fill(lines['content'].encode('utf-8').strip()) + '\n') if lines['image'] != '': from PIL import Image from io import BytesIO data = lines['image'] img = Image.open(BytesIO(base64.b64decode(data))) basewidth = 384 wpercent = (basewidth/float(img.size[0])) hsize = int((float(img.size[1])*float(wpercent))) img = img.resize((basewidth,hsize), PIL.Image.ANTIALIAS) img = img.convert('1') w, h = img.size p.justify('C') p.print_bitmap(img.getdata(), w, h, True) p.justify() p.justify('R') p.print_text('--------' + '\n') p.font_b() p.print_text(lines['from'].encode('utf-8').strip() + '\n') p.font_b(False) #print lines['from'] p.justify('R') p.font_b() p.print_text(lines['meta'].encode('utf-8').strip() + '\n') p.font_b(False) #print lines['meta'] p.justify('L') p.print_text('________________________________') p.linefeed() p.linefeed() p.linefeed()
def test_print(self, image_path): # プリンタ準備 p = printer.ThermalPrinter(heatingDots=0, heatTime=255, heatInterval=255) # resize img = cv.imread(image_path) #ih,iw,ic = img.shape[:3] #new_h = ih*(iw/384) #img = cv.resize(img,dsize=(384,new_h)) img = cv.resize(img,dsize=(384,537)) img = cv.rotate(img, cv.ROTATE_180) cv.imwrite("tmp.jpg", img) #i = Image.open("tmp.jpg").convert('1') i = Image.open("tmp.jpg") # edge only #i1 = i.convert('L') #i2 = i1.filter(ImageFilter.MaxFilter(5)) #i3 = ImageChops.difference(i1,i2) #output_image = ImageOps.invert(i3) #i = ImageOps.equalize(i) #i = i.convert('1') #output_image = i.convert('L') output_image = i.convert('1') output_image.save("./tmp.jpg") data = list(output_image.getdata()) w, h = output_image.size p.set_param() p.print_bitmap(data, w, h) # 少し紙送り p.linefeed(3)
import requests import schedule import time import re import os import subprocess import cStringIO import printer from PIL import Image, ImageDraw # TODO: Edit zj-58 driver to make the media size smaller? subprocess.call(["stty", "-F", "/dev/ttyUSB0", "19200"]) #subprocess.call(["stty", "-F", "/dev/ttyAMA1", "19200"]) p = printer.ThermalPrinter() #p = printer.ThermalPrinter(serialport="/dev/ttyAMA1") def print_messages(): print("hello") # r = requests.get("http://localhost:3001/messages") r = requests.get("https://send-nate-a-message.herokuapp.com/messages") for message in r.json(): id = message["id"] print(message["id"]) print(message["text"]) print(message["image"]) image_data = re.sub('^data:image/.+;base64,', '', message["image"]).decode('base64')
#!/usr/local/bin/python2 # coding:utf-8 import printer from PIL import Image import sys port = "/dev/cuaU0" #brate = 115200 brate = 38400 #brate = 19200 if len(sys.argv) > 1: imagefile = sys.argv[1] if len(sys.argv) > 2: port = sys.argv[2] if len(sys.argv) > 3: brate = int(sys.argv[3]) else: print("File name ") exit(1) p = printer.ThermalPrinter(serialport=port, baudrate=brate) i = Image.open(imagefile) data = list(i.getdata()) w, h = i.size p.print_bitmap(data, w, h) p.linefeed(3)
import printer, textwrap, sys # Pi 1 and 2 # p = printer.ThermalPrinter(serialport="/dev/ttyAMA0") # Pi 3 p = printer.ThermalPrinter(serialport="/dev/ttyS0") unwrapped_text = "\n From My Pi, Roses are red, violets are blue, I'm seeing a future filled with Poems and you!\n" wrapped_text = textwrap.fill(unwrapped_text, 32) p.print_text(wrapped_text) p.linefeed() p.linefeed() p.linefeed()