Python|自制二维码生成器

时间:2022-07-23
本文章向大家介绍Python|自制二维码生成器,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1 前言

在如今信息发达的时代,二维码已经是人们生活中不可或缺的东西。比如几乎每天都要用的微信或支付宝支付。那么如何可以制作一个二维码呢?小编将在本文中给大家分享一个自制的二维码生成器。

2准备

这个二维码生成器是由qrcode(生成二维码)库与tkinter(图形ui界面)组成的。首先先在命令行安装以下三个模块,分别是qrcode、image、pillow(PIL)。安装方式很简单。

pip install qrcode

pip install image

pip install pillow

安装完整过后直接在py文件中导入以下模块和方法:

from tkinter import *

from tkinter.filedialog import *

from PIL import Image,ImageTk

import qrcode

3具体步骤

3.1编写ui界面

导入模块后直接用tkinter模块编写ui界面。小编这里的ui界面为:

图3.1ui界面

具体代码如下:

root = Tk()

root.title("二维码生成器")

root.geometry('600x400+400+100')

button1 = Button(root,text = '选择图标',font = ('宋体',20),fg = 'green',bg = 'white',command = openfile)#设置按钮

button2 = Button(root,text = '保存二维码',font = ('宋体',20),fg = 'green',bg = 'white',command = savefile)#设置按钮

button1.place(x = 90,y = 330,width = 120,height = 50)#显示按钮

button2.place(x = 385,y = 330,width = 150,height = 50)#显示按钮

label1 = Label(root,text = '输入链接',font = ('宋体',20),fg = 'black',bg = 'white')#设置组件

label1.place(x = 235,y = 5,width = 130,height = 50)

entry1 = Entry(root,font = ('宋体',20))#设置输入框

entry1.place(x = 50,y = 60,width = 510,height = 30)#显示组件

canvas1 = Canvas(root,width = 300,height = 300,bg = "white")#创建画布

canvas2 = Canvas(root,width = 300,height = 300,bg = "white")#创建画布

canvas1.place(x = 50,y = 100,width = 200,height = 200)

canvas2.place(x = 360,y = 100,width = 200,height = 200)

button = Button(root,text = '生成',font = ('宋体',15),fg = 'black',bg = 'pink',command = creat)#设置按钮

button.place(x = 280,y = 200,width = 50,height = 40)#显示按钮

root.mainloop()

Tkinter的基础用法此公众号内有相关用法,可以搜索关键词tkinter阅读。

这里只简单说一下部分方法及参数的含义。

Button()方法为创建一个按钮组件,其中command为点击按钮绑定的事件(函数方法)。

place()为一种布局方式,参数x,y为相对ui界面的坐标,width和height为显示宽高。

Label()为显示文字组件,例如图3.1中的“输入链接”。

Entry()为输入框组件,这里用于接收链接。使用entry.get()获取其中的内容。

Canvas()为画布组件,这里用于展示图标和二维码。

font参数为字体。其中可以设置字体样式和大小。

3.2生成二维码

程序的ui界面就已经写好了,最后只需要完成按钮中的comman参数就好了。分别有三个方法。先来看选择图标。

def openfile():

    global filename,image_name

    filename = askopenfilename()

    image_name = Image.open(filename)

    image_name = image_name.resize((200, 200), Image.ANTIALIAS)#缩放图片

    im_root = ImageTk.PhotoImage(image_name)  # 预设打开的图片

    canvas1.create_image(100,100,image=im_root)  # 嵌入预设的图片

    canvas1.place(x = 50,y = 100,width = 200,height = 200)

    root.mainloop()

这里面只说一下askopenfilename(),这是tikinter模块中filedialog类的一个方法,返回的是你当前选择文件的路径。然后利用image模块将此图片打开并按照要求缩放,最终展示在画布上。

图3.2选取图片

图3.3展示图片

然后是生成函数:

def creat():

    global img

    qr = qrcode.QRCode(

        version=2,

        error_correction=qrcode.constants.ERROR_CORRECT_Q,

        box_size=10,

        border=1)

    url = entry1.get()

    qr.add_data(url)

    qr.make(fit=True)

    img = qr.make_image()

    img = img.convert("RGBA")

    icon = image_name

    icon = icon.convert("RGBA")

    imgWight, imgHeight = img.size

    iconWight = int(imgWight / 3)

    iconHeight = int(imgHeight / 3)

    icon = icon.resize((iconWight, iconHeight), Image.ANTIALIAS)

    posW = int((imgWight - iconWight) / 2)

    posH = int((imgHeight - iconHeight) / 2)

    img.paste(icon, (posW, posH), icon)

    img1 = img.resize((200, 200), Image.ANTIALIAS)

    im_root = ImageTk.PhotoImage(img1)  # 预设打开的图片

    canvas2.create_image(100,100,image=im_root)  # 嵌入预设的图片

    canvas2.place(x = 360,y = 100,width = 200,height = 200)

    root.mainloop()

其中qr部分为二维码的配置。

version参数是从1到40,其控制QR码的大小的整数(最小的,版本1,是一个21×21矩阵)。设置为None并在使代码自动确定时使用fit参数。

error_correction参数控制用于QR码的误差校正。在qrcode 软件包中提供了以下四个常量:

ERROR_CORRECT_L

可以纠正大约7%或更少的错误。

ERROR_CORRECT_M(默认)

可以纠正大约15%或更少的错误。

ERROR_CORRECT_Q

可以纠正大约25%或更少的错误。

ERROR_CORRECT_H。

可以纠正大约30%或更少的错误。

box_size参数控制每个二维码格子中有多少个像素。

border参数控制边界应多少盒厚是(默认为4,这是最低根据规范)。

add_data()为二维码的链接,这里直接获取输入框中的内容。

然后后面的内容都为控制图标与二维码的相对大小和位置。以上这部分的参数均来自qrcode的官方文档。详情请到官网查看:https://pypi.org/project/qrcode/5.1/

该方法写好后输入链接,点击生成,就可以生成一个带图标的二维码了。

图3.4生成二维码

最后是保存二维码:

def savefile():

    pathname = asksaveasfilename(defaultextension = '.png',initialfile = '新的二维码.png')

    img.save(pathname)

其中的asksavesfilename同样是返回文件保存的路径,后面两个参数依次是默认图片格式、默认文件名。最后点击保存二维码即可大功告成。

图3.5保存二维码

最后打开保存的文件夹,检查一下,发现成功生成了二维码。

3.6查看二维码

4完整代码

from tkinter import *

from tkinter.filedialog import *

from PIL import Image,ImageTk

import qrcode

def openfile():

    global filename,image_name

    filename = askopenfilename()

    image_name = Image.open(filename)

    image_name = image_name.resize((200, 200), Image.ANTIALIAS)#缩放图片

    im_root = ImageTk.PhotoImage(image_name)  # 预设打开的图片

    canvas1.create_image(100,100,image=im_root)  # 嵌入预设的图片

    canvas1.place(x = 50,y = 100,width = 200,height = 200)

    root.mainloop()

def creat():

    global img

    qr = qrcode.QRCode(

        version=2,

        error_correction=qrcode.constants.ERROR_CORRECT_Q,

        box_size=10,

        border=1)

    url = entry1.get()

    qr.add_data(url)

    qr.make(fit=True)

    img = qr.make_image()

    img = img.convert("RGBA")

    icon = image_name

    icon = icon.convert("RGBA")

    imgWight, imgHeight = img.size

    iconWight = int(imgWight / 3)

    iconHeight = int(imgHeight / 3)

    icon = icon.resize((iconWight, iconHeight), Image.ANTIALIAS)

    posW = int((imgWight - iconWight) / 2)

    posH = int((imgHeight - iconHeight) / 2)

    img.paste(icon, (posW, posH), icon)

    img1 = img.resize((200, 200), Image.ANTIALIAS)

    im_root = ImageTk.PhotoImage(img1)  # 预设打开的图片

    canvas2.create_image(100,100,image=im_root)  # 嵌入预设的图片

    canvas2.place(x = 360,y = 100,width = 200,height = 200)

    root.mainloop()

def savefile():

    pathname = asksaveasfilename(defaultextension = '.png',initialfile = '新的二维码.png')

    img.save(pathname)

root = Tk()

root.title("二维码生成器")

root.geometry('600x400+400+100')

button1 = Button(root,text = '选择图标',font = ('宋体',20),fg = 'green',bg = 'white',command = openfile)#设置按钮

button2 = Button(root,text = '保存二维码',font = ('宋体',20),fg = 'green',bg = 'white',command = savefile)#设置按钮

button1.place(x = 90,y = 330,width = 120,height = 50)#显示按钮

button2.place(x = 385,y = 330,width = 150,height = 50)#显示按钮

label1 = Label(root,text = '输入链接',font = ('宋体',20),fg = 'black',bg = 'white')#设置组件

label1.place(x = 235,y = 5,width = 130,height = 50)

entry1 = Entry(root,font = ('宋体',20))#设置输入框

entry1.place(x = 50,y = 60,width = 510,height = 30)#显示组件

canvas1 = Canvas(root,width = 300,height = 300,bg = "white")#创建画布

canvas2 = Canvas(root,width = 300,height = 300,bg = "white")#创建画布

canvas1.place(x = 50,y = 100,width = 200,height = 200)

canvas2.place(x = 360,y = 100,width = 200,height = 200)

button = Button(root,text = '生成',font = ('宋体',15),fg = 'black',bg = 'pink',command = creat)#设置按钮

button.place(x = 280,y = 200,width = 50,height = 40)#显示按钮

root.mainloop()

最后你还可用小编之前分享过的关于Python文件打包的方法,将该程序打包成exe文件,方便自己和他人使用。

END