Add printing to mqtt

This commit is contained in:
jimmy 2022-07-23 01:40:33 +12:00
parent e784983f99
commit 1dd5117e1f
1 changed files with 54 additions and 12 deletions

View File

@ -1,5 +1,7 @@
import StarTSPImage
from PIL import Image, ImageDraw
from PIL import Image, ImageDraw, ImageFont, ImageOps
import textwrap
import random
@ -15,24 +17,64 @@ def on_connect(client, userdata, flags, rc):
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
image = Image.new('RGB', (2000, 2000), color='White')
draw = ImageDraw.Draw(image)
draw.ellipse((0, 0, 500, 500), fill='Black')
draw.ellipse((10, 10, 490, 490), fill='White')
image.show()
image.save("image.jpg")
with open("printer/locations.txt") as locations:
lines = locations.read().splitlines()
location = random.choice(lines)
# raster = StarTSPImage.imageToRaster(image, cut=True)
with open("printer/actions.txt") as actions:
lines = actions.read().splitlines()
action = random.choice(lines)
with open("printer/adverbs.txt") as adverb:
lines = adverb.read().splitlines()
adverb = random.choice(lines)
if random.randint(0,1):
im=Image.open("printer/image.jpg")
else:
im=Image.open("printer/image2.jpg")
# f = ImageFont.load_default()
f = ImageFont.truetype("printer/SilkRemington-Regular.otf", 40)
txt=Image.new('L', (1000,230))
d = ImageDraw.Draw(txt)
text = "While {}, {}, {}".format(location, action, adverb)
text_short = "By the DJ, offer a stranger a drink"
text_long = "In the outdoor area, ask someone would you rather be known for always being honest or always being kind"
lines = textwrap.wrap(text, width=23)
if len(lines) > 2:
im=Image.open("printer/image.jpg")
else:
im=Image.open("printer/image2.jpg")
y = 0
n = 0
for line in lines:
n += 1
width, height = f.getsize(line)
d.text(((1000-width)/2, y), line, font=f, fill=255)
y += height
w=txt.rotate(-90, expand=1)
print(n)
im.paste( ImageOps.colorize(w, (0,0,0), (0,0,0)), ( (n-5)*20 + 127, 5), w)
im.show()
im.save("image_tmp.jpg")
raster = StarTSPImage.imageToRaster(im, cut=True)
printer = open('/dev/usb/lp0', "wb")
#printer.write(raster)
# printer = open('/dev/usb/lp0', "wb")
# printer.write(raster)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("10.1.1.162", 1883, 60)
client.connect("", 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.