-
Notifications
You must be signed in to change notification settings - Fork 0
/
print-v3.py
103 lines (78 loc) · 2.64 KB
/
print-v3.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from PIL import Image, ImageDraw, ImageFont,ImageWin
import qrcode
import win32print
import win32ui
import time
import datetime
date = time.strftime("%Y-%m-%d %H:%M:%S")
def comb(png1, png2, style='horizontal'):
img1, img2 = Image.open(png1), Image.open(png2)
# 统一图片尺寸,可以自定义设置(宽,高)
img1 = img1.resize((550, 200), Image.ANTIALIAS) #以第一张图片高度为准
img2 = img2.resize((100, 100), Image.ANTIALIAS)
size1, size2 = img1.size, img2.size
if style == 'horizontal':
joint = Image.new('RGB', (size1[0] + size2[0], size1[1]))
loc1, loc2 = (100, 0), (0, 50)
joint.paste(img1, loc1)
joint.paste(img2, loc2)
joint.save('putout.png')
elif style == 'vertical':
joint = Image.new('RGB', (size1[0], size1[1] + size2[1]))
loc1, loc2 = (100, 0), (50,0)
joint.paste(img1, loc1)
joint.paste(img2, loc2)
joint.save('putout1.png')
if __name__ == '__main__':
# 两张图片地址:
png1 = r"./99999.png"
png2 = r"./--100.png"
# 左右拼接
comb(png1, png2, style='horizontal')
# 上下拼接
# comb(png1, png2, style='vertical')
# 打开图片
image = Image.open('putout.png')
# 在右侧添加文字
draw = ImageDraw.Draw(image)
text = "No." + date
font = ImageFont.truetype('arial.ttf', 30) # 修改字体和大小
text_width, text_height = draw.textsize(text, font)
x = image.width - text_width - 230 # 文字位置,可以自行调整
y = image.height // 2 - text_height // 2
draw.text((x, y), text, font=font, fill='black')
# draw.text((x, y), text, font=font, fill='white')
# 显示图片
image.show()
# 保存图片
image.save('final.png')
# 使用打印机打印图片
# 列出所有打印机
# printers = [printer[2] for printer in win32print.EnumPrinters(2)]
# for i, printer in enumerate(printers):
# print(f"{i+1}: {printer}")
# 选择打印机
# choice = int(input("选择要使用的打印机 (输入对应的序号): ")) - 1
# printer_name = printers[choice]
# 获取默认打印机
printer_name = win32print.GetDefaultPrinter()
# 加载图片
image_path = r"final.png"
image = Image.open(image_path)
# 创建设备描述表
hDC = win32ui.CreateDC()
hDC.CreatePrinterDC(printer_name)
# 开始文档
hDC.StartDoc(image_path)
# 开始页面
hDC.StartPage()
# 绘制位图
dib = ImageWin.Dib(image)
dib.draw(hDC.GetHandleOutput(), (0, 0, image.width, image.height))
# 结束页面
hDC.EndPage()
# 结束文档
hDC.EndDoc()
# 删除设备描述表对象
del hDC
print("打印成功!")