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制造二維碼生成器的代碼過程
我是白白,一個喜歡學習喜歡編程的年輕人
想學習python的可以關注私信我哦~
歡迎小白、萌新、大佬加入我們
小白學習交流基地【(九七四)(七二四)(八九四)】