1、生成带logo的二维码图片并且保存
前提条件:在D盘里有logo.png的图片,生成的二维码图片在D盘里的111.png
import qrcode
from PIL import Image
# 前提条件:在D盘里有logo.png的图片,生成的二维码图片在D盘里的111.png
def create_qrcode(url, filename):
qr = qrcode.QRCode(
version=1,
# 设置容错率为最高
error_correction=qrcode.ERROR_CORRECT_H,
box_size=10,
border=4,
)
qr.add_data(url)
qr.make(fit=True)
img = qr.make_image()
# 设置二维码为彩色
img = img.convert("RGBA")
icon = Image.open('D:/logo.png')
w, h = img.size
factor = 4
size_w = int(w / factor)
size_h = int(h / factor)
icon_w, icon_h = icon.size
if icon_w > size_w:
icon_w = size_w
if icon_h > size_h:
icon_h = size_h
icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)
w = int((w - icon_w) / 2)
h = int((h - icon_h) / 2)
icon = icon.convert("RGBA")
# 这里可以修改宽度
newimg = Image.new("RGBA", (icon_w + 5, icon_h + 5), (255, 255, 255))
img.paste(newimg, (w - 4, h - 4), newimg)
img.paste(icon, (w, h), icon)
img.save('D:/' + filename + '.png', quality=100)
if __name__ == "__main__":
create_qrcode('https://blog.csdn.net/wy313622821?spm=1011.2124.3001.5343', "111")
2、控制打印机打印图片二维码
背景:需要打印二维码图片,保证手动能够打开图片并且点打印可以打印出来(安装了对应的打印机驱动)
代码为:
import win32print
import win32ui
from PIL import Image, ImageWin
#
# Constants for GetDeviceCaps
#
#
# HORZRES / VERTRES = printable area
#
HORZRES = 30
VERTRES = 30
#
# LOGPIXELS = dots per inch
#
# LOGPIXELSX = 40
# LOGPIXELSY = 45
#
# PHYSICALWIDTH/HEIGHT = total area
#
PHYSICALWIDTH = 30
PHYSICALHEIGHT = 30
#
# PHYSICALOFFSETX/Y = left / top margin
#
PHYSICALOFFSETX = 112
PHYSICALOFFSETY = 113
printer_name = win32print.GetDefaultPrinter()
file_name = "test.jpg"
#
# You can only write a Device-independent bitmap
# directly to a Windows device context; therefore
# we need (for ease) to use the Python Imaging
# Library to manipulate the image.
#
# Create a device context from a named printer
# and assess the printable size of the paper.
#
hDC = win32ui.CreateDC()
hDC.CreatePrinterDC(printer_name)
printable_area = hDC.GetDeviceCaps(HORZRES), hDC.GetDeviceCaps(VERTRES)
printer_size = hDC.GetDeviceCaps(PHYSICALWIDTH), hDC.GetDeviceCaps(PHYSICALHEIGHT)
printer_margins = hDC.GetDeviceCaps(PHYSICALOFFSETX), hDC.GetDeviceCaps(PHYSICALOFFSETY)
#
# Open the image, rotate it if it's wider than
# it is high, and work out how much to multiply
# each pixel by to get it as big as possible on
# the page without distorting.
#
bmp = Image.open(file_name)
if bmp.size[0] > bmp.size[1]:
bmp = bmp.rotate(90)
ratios = [1.0 * printable_area[0] / bmp.size[0], 1.0 * printable_area[1] / bmp.size[1]]
scale = min(ratios)
#
# Start the print job, and draw the bitmap to
# the printer device at the scaled size.
#
hDC.StartDoc(file_name)
hDC.StartPage()
dib = ImageWin.Dib(bmp)
scaled_width, scaled_height = [int(scale * i) for i in bmp.size]
x1 = int((printer_size[0] - scaled_width) / 2)
y1 = int((printer_size[1] - scaled_height) / 2)
x2 = x1 + scaled_width
y2 = y1 + scaled_height
dib.draw(hDC.GetHandleOutput(), (x1, y1, x2, y2))
hDC.EndPage()
hDC.EndDoc()
hDC.DeleteDC()
3、整合
import tkinter
import win32print
import win32ui
from PIL import Image, ImageWin
import qrcode
import pyperclip
# 前提条件:在D盘里有logo.png的图片,生成的二维码图片在D盘里的111.png
# 打包成exe文件:1、在Terminal中安装pip install pyinstaller
# 2、在Terminal中 pyinstaller.exe -F print2.py,把print2.py文件放到build文件夹里
# 3、生成的exe就放在dis里面
#
# 使用字符串生成二维码图片
#
def create_qrcode(url, filename):
qr = qrcode.QRCode(
version=1,
# 设置容错率为最高
error_correction=qrcode.ERROR_CORRECT_H,
box_size=10,
border=4,
)
qr.add_data(url)
qr.make(fit=True)
img = qr.make_image()
# 设置二维码为彩色
img = img.convert("RGBA")
# icon = Image.open('D:/logo.png')
icon = Image.open('logo.png')
w, h = img.size
factor = 4
size_w = int(w / factor)
size_h = int(h / factor)
icon_w, icon_h = icon.size
if icon_w > size_w:
icon_w = size_w
if icon_h > size_h:
icon_h = size_h
icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)
w = int((w - icon_w) / 2)
h = int((h - icon_h) / 2)
icon = icon.convert("RGBA")
# 这里可以修改宽度
newimg = Image.new("RGBA", (icon_w + 5, icon_h + 5), (255, 255, 255))
img.paste(newimg, (w - 4, h - 4), newimg)
img.paste(icon, (w, h), icon)
img.save(filename + '.png', quality=100,dpi=(600.0,600.0))
#
# 使用默认打印机打印二维码图片
#
def print_jiabo():
#
# HORZRES / VERTRES = printable area
#
#原本 30 30
HORZRES = 30
VERTRES = 30
#
# LOGPIXELS = dots per inch
#
# LOGPIXELSX = 40
# LOGPIXELSY = 45
#
# PHYSICALWIDTH/HEIGHT = total area
#
# 74:DA:EA:94:0D:0A
PHYSICALWIDTH = 30
PHYSICALHEIGHT = 30
#
# PHYSICALOFFSETX/Y = left / top margin
#
PHYSICALOFFSETX = 113
PHYSICALOFFSETY = 113
printer_name = win32print.GetDefaultPrinter()
file_name = "zilaijian.png"
#
# You can only write a Device-independent bitmap
# directly to a Windows device context; therefore
# we need (for ease) to use the Python Imaging
# Library to manipulate the image.
#
# Create a device context from a named printer
# and assess the printable size of the paper.
#
hDC = win32ui.CreateDC()
hDC.CreatePrinterDC(printer_name)
printable_area = hDC.GetDeviceCaps(HORZRES), hDC.GetDeviceCaps(VERTRES)
printer_size = hDC.GetDeviceCaps(PHYSICALWIDTH), hDC.GetDeviceCaps(PHYSICALHEIGHT)
printer_margins = hDC.GetDeviceCaps(PHYSICALOFFSETX), hDC.GetDeviceCaps(PHYSICALOFFSETY)
#
# Open the image, rotate it if it's wider than
# it is high, and work out how much to multiply
# each pixel by to get it as big as possible on
# the page without distorting.
#
bmp = Image.open(file_name)
if bmp.size[0] > bmp.size[1]:
bmp = bmp.rotate(90)
# ratios = [1.0 * printable_area[0] / bmp.size[0], 1.0 * printable_area[1] / bmp.size[1]]
ratios = [0.85 * printable_area[0] / bmp.size[0], 0.85 * printable_area[1] / bmp.size[1]]
scale = min(ratios)
#
# Start the print job, and draw the bitmap to
# the printer device at the scaled size.
# 20 CD 39 9C CD 8E
#
hDC.StartDoc(file_name)
hDC.StartPage()
dib = ImageWin.Dib(bmp)
scaled_width, scaled_height = [int(scale * i) for i in bmp.size]
x1 = int((printer_size[0] - scaled_width) / 2)
y1 = int((printer_size[1] - scaled_height) / 2)
x2 = x1 + scaled_width
y2 = y1 + scaled_height
# (10,10,1024,768)前面的两个数字是坐标,后面两个数字是打印纸张的大小(注意图片白边)
# dib.draw(hDC.GetHandleOutput(), (x1, y1, x2, y2))
# print("====>",x1, y1, x2, y2)
dib.draw(hDC.GetHandleOutput(), (11, 10, x2, y2))
hDC.EndPage()
hDC.EndDoc()
hDC.DeleteDC()
def start(deviceID):
# print("设备id==:", deviceID.replace(" ", ":"))
deviceID_m = deviceID.replace(" ", ":")
# 把设备id设置到 粘贴板(这一句不要可以删除,不影响其他)
pyperclip.copy(deviceID_m)
# create_qrcode('https://iot.fslihua.cn/aixiang/M6z7uBqbPj.txt?deviceId=20:CD:39:B4:E8:6D', "zilaijian")
create_qrcode('https://iot.fslihua.cn/aixiang/M6z7uBqbPj.txt?deviceId=' + deviceID_m, "zilaijian")
print_jiabo()
if __name__ == '__main__':
# 界面****begin
window = tkinter.Tk()
window.geometry("300x100")
t1 = tkinter.StringVar()
E1 = tkinter.Entry(window, textvariable=t1, bd=3)
E1.pack()
# 获取输入框的值:E1.get()
B = tkinter.Button(window, text="开始打印", command=lambda: start(E1.get()))
B.pack()
L1 = tkinter.Label(window, text="请把设备的ID复制到上面的输入框里")
L1.pack()
window.mainloop()
# 界面****end
4、打包成exe文件
1、在Terminal中安装pip install pyinstaller
2、在Terminal中 pyinstaller.exe -F print2.py,把print2.py文件放到build文件夹里,不要黑色的空窗口的打包:pyinstaller.exe -w -F print2.py
总结命令:
Pyinstaller -F setup.py 打包exe
Pyinstaller -F -w setup.py 不带控制台的打包
Pyinstaller -F -i xx.ico setup.py 打包指定exe图标打包
3、生成的exe就放在dis里面