forked from swoolf/DocPB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer.py
76 lines (60 loc) · 2.02 KB
/
printer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import cups
from PIL import Image, ImageFont, ImageDraw
from io import BytesIO
import base64
import re
from datetime import date, datetime
temp_folder = "temp/"
prints_folder = "static/prints/"
original_file = "example.png"
collage_file = "collage.jpg"
caption = "Brookline, Massachusetts - %s"
def saveImage(b64image, counter):
filepath = temp_folder + str(counter) + ".png"
data = re.sub('^data:image/.+;base64,', '', b64image)
image = Image.open(BytesIO(base64.b64decode(data)))
image.save(filepath)
def assemblePrint(image_filepaths):
pwidth = 1500
pheight=1000
extra = 30
iheight = 450
iwidth = int(iheight*640/480)
offset = 640-iwidth
border = 10
images= []
for filepath in image_filepaths:
image = Image.open(filepath)
images.append(image)
for i in range (0,4):
images[i].thumbnail((iwidth,iheight))
forPrint = Image.new("RGB", (pwidth, pheight), "white")
forPrint.paste(images[0],(offset+20,35))
forPrint.paste(images[1],(offset+20,55+iheight))
forPrint.paste(images[2],(offset+40+iwidth,35))
forPrint.paste(images[3],(offset+40+iwidth,55+iheight))
title = Image.open('border.jpg')
title.thumbnail((1000,1000))
forPrint.paste(title,(offset+50+iwidth*2,10))
# font = ImageFont.truetype('assets/Verdana.ttf', 35)
# draw = ImageDraw.Draw(forPrint)
# draw.text((offset+20, 930),caption % (date.today().strftime("%m/%d/%Y")),font=font, fill="#000000" )
fheight=int(pheight+extra)
fwidth=int(fheight*1.5)
cropPrint = Image.new("RGB", (fwidth, fheight), "white")
cropPrint.paste(forPrint, (int(0.75*extra),int(0.5*extra)))
filename = prints_folder + datetime.now().strftime("%Y_%m_%d-%H_%M_%S") + ".jpg"
cropPrint.save(filename)
return filename
def sendToPrinter(collage_path):
conn = cups.Connection()
printers = conn.getPrinters()
printer_name = None
for name in printers.keys():
if ('selphy' in name.lower()):
printer_name = name
if printer_name:
conn.printFile(printer_name, collage_path, "PhotoBooth",{"copies": str(1)})
print("Printing photo...")
else:
print("I can't find the printer...")