python

您所在的位置:网站首页 二维码图片怎样打印 python

python

2024-07-16 01:30:38| 来源: 网络整理| 查看: 265

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里面



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭