techoverload/printer/test.py

57 lines
1.5 KiB
Python
Raw Normal View History

2022-07-21 06:16:33 +00:00
import StarTSPImage
from PIL import Image, ImageDraw, ImageFont, ImageOps
import textwrap
2022-07-21 07:48:30 +00:00
import random
2022-07-21 06:16:33 +00:00
2022-09-24 00:53:32 +00:00
print("Print")
2022-07-21 07:48:30 +00:00
with open("printer/locations.txt") as locations:
lines = locations.read().splitlines()
location = random.choice(lines)
2022-07-21 06:16:33 +00:00
2022-07-21 07:48:30 +00:00
with open("printer/actions.txt") as actions:
lines = actions.read().splitlines()
action = random.choice(lines)
2022-07-21 06:16:33 +00:00
2022-07-21 07:48:30 +00:00
with open("printer/adverbs.txt") as adverb:
lines = adverb.read().splitlines()
adverb = random.choice(lines)
2022-07-21 06:16:33 +00:00
2022-09-24 00:53:32 +00:00
if random.randint(0,1):
im=Image.open("printer/image.jpg")
else:
im=Image.open("printer/image2.jpg")
2022-07-21 06:16:33 +00:00
# f = ImageFont.load_default()
2022-07-21 06:58:27 +00:00
f = ImageFont.truetype("printer/SilkRemington-Regular.otf", 40)
txt=Image.new('L', (1000,230))
2022-07-21 06:16:33 +00:00
d = ImageDraw.Draw(txt)
2022-07-21 07:48:30 +00:00
text = "While {}, {}, {}".format(location, action, adverb)
2022-07-21 06:16:33 +00:00
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"
2022-07-21 07:48:30 +00:00
lines = textwrap.wrap(text, width=23)
2022-07-21 06:16:33 +00:00
2022-09-24 00:53:32 +00:00
if random.randint(0,1) == 0:
2022-07-21 07:16:18 +00:00
im=Image.open("printer/image.jpg")
else:
im=Image.open("printer/image2.jpg")
2022-07-21 06:16:33 +00:00
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)
2022-07-21 07:16:18 +00:00
im.paste( ImageOps.colorize(w, (0,0,0), (0,0,0)), ( (n-5)*20 + 127, 5), w)
2022-07-21 06:16:33 +00:00
2022-09-24 00:53:32 +00:00
# im.show()
# im.save("image_tmp.jpg")
2022-07-21 06:16:33 +00:00
raster = StarTSPImage.imageToRaster(im, cut=True)
printer = open('/dev/usb/lp0', "wb")
2022-09-24 00:53:32 +00:00
printer.write(raster)