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.sizefactor = 4size_w = int(w / factor)size_h = int(h / factor)icon_w, icon_h = icon.sizeif icon_w > size_w:icon_w = size_wif icon_h > size_h:icon_h = size_hicon = 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 = 113printer_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.sizefactor = 4size_w = int(w / factor)size_h = int(h / factor)icon_w, icon_h = icon.sizeif icon_w > size_w:icon_w = size_wif icon_h > size_h:icon_h = size_hicon = 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  30HORZRES = 30VERTRES = 30## LOGPIXELS = dots per inch## LOGPIXELSX = 40# LOGPIXELSY = 45## PHYSICALWIDTH/HEIGHT = total area## 74:DA:EA:94:0D:0APHYSICALWIDTH = 30PHYSICALHEIGHT = 30## PHYSICALOFFSETX/Y = left / top margin#PHYSICALOFFSETX = 113PHYSICALOFFSETY = 113printer_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_widthy2 = 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__':# 界面****beginwindow = 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里面

python——生成带logo的二维码图片并且保存、控制打印机打印图片二维码、整合打印(获取输入框的值)、打包成exe文件相关推荐

  1. python 打包 小文件_[Python][小知识][NO.5] 使用 Pyinstaller 打包成.exe文件

    1.安装 pyinstaller 插件 cmd命令:pip install PyInstaller PS . o.o 不知道 easy_install 的百度吧. 2.pyinstaller 简介 他 ...

  2. 将python打包成exe文件(携带附属文件)

    前言 用Pyinstaller进行打包exe时,有时候得附带上一些资源,提高程序的实用性和人性化. 在网上也有很多把依赖文件打包进exe的方法,不过都只能打包一些图片或者文本而已. 另一种方法是通过修 ...

  3. python文件图标变成小电脑_手把手教你给Python程序写图形界面,并且打包成exe文件-exe文件...

    环境配置 官网下载Python3,LZ的配置环境是Python3.6,PyCharm 2017.2.1pip3 install PyQt5 #下载PyQt5 pip install PyQt5-too ...

  4. Python打包成exe文件_详细操作

    Python打包成exe文件 前言 一.安装pyinstaller 1.1 安装pyinstaller,使用安装命令: 1.2 如果遇到需要更新版本请输入: 1.3 检查是否正确安装 1.4 稍等,水 ...

  5. 解决 Python打包成exe 文件过大问题的一些方法

    前言 之前有做过Python的pyqt桌面应用,当时每次更新打包的时候整个文件下来都需要300~400M,但是一直没有找到合适的方法解决,而是尽量Python少安抓库包,但效果一般,最近找到了解决方法 ...

  6. 把python语言翻译出来_Python语言实现翻译小工具(Python打包成exe文件)

    本文主要向大家介绍了Python语言实现翻译小工具(Python打包成exe文件),通过具体的内容向大家展示,希望对大家学习Python语言有所帮助. 1.环境 windows10 python3.5 ...

  7. python 程序打包 vscode_使用VScode编写python程序并打包成.exe文件

    听说Visual Studio Code(VS Code)的诸多好处,了解了一下果真很喜欢,我喜欢它的缘由主要有3个,一是VS Code开源且跨平台,二是由于其界面很是酷,三是能够知足个人大所属代码需 ...

  8. python打包成.exe文件时出现“系统找不到指定路径”

    python打包成.exe文件时出现"系统找不到指定路径" 我在一开始写工程时就想到最后打包的时候可能会出现文件位置会发生移动,所以并没有使用绝对路径,而都是以相对路径写的程序. ...

  9. python如何将图片打包进exe里_史上最详细的Python打包成exe文件教程

    打包成exe文件可以让python代码在没有python环境的条件下,依然能够运行,实在是码农们写追女朋友表白.情人节浪漫的必需品! 1.使用豆瓣镜像源下载: pyinstaller 有需要了解如何使 ...

最新文章

  1. ssh时出现 Agent admitted failure to sign using the key
  2. Android Studio 代码自动提示无效
  3. 为何我通过 Calendar1.SelectedDate.DayOfWeek.ToString() 获取的“星期”总是英文的???...
  4. kubesphere3.0的安装完整文档
  5. POJ 2299 - Ultra-QuickSort BIT
  6. Hadoop学习之路(十三)MapReduce的初识
  7. Spring 创建对象的方式
  8. OpenShift 4 - 为客户端配置使用基于CA证书的kubeconfig实现无密码登录
  9. LabView学习笔记(八):属性节点
  10. nodejs-ORM 框架 waterline和Sails
  11. 怎样在计算机上注册dll文件,注册dll文件【搞定步骤】
  12. Shiro-单点登录原理
  13. 汽车之家字体反爬破解实践
  14. html5文字游戏制作工具,橙光文字游戏制作工具
  15. 王家林的Hadoop之旅
  16. 关于学习的三个认知升级
  17. 【华为OD机试真题 Python】判断字符串子序列
  18. 解决wordcloud导出图片不清楚
  19. 网页上显示word和Excel
  20. 【工业机器人】关于工业机器人控制系统,这几方面内容你必须掌握;如何将工业机器人与数控机床融合应用?

热门文章

  1. HLSL 学习笔记3 法线映射
  2. Java平台分成几类?各自的使用范围是什么?
  3. Python爬虫大师班
  4. node进阶学习(express)
  5. 机遇挑战药食同源健康产业论坛 万祥军:黑龙江工商联主导
  6. Python中的水平制表符:\t
  7. Laravel中错误页面的显示
  8. canvans 粒子背景特效
  9. Kitematic安装Elasticsearch以及启动遇到的问题
  10. VMware 安装centos7 不带桌面的,命令行的那种