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保存二維碼
最后打開保存的檔案夾,檢查一下,發現成功生成了二維碼,
https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzI5MTQ5NDY1MA==&action=getalbum&album_id=1309649394022645761&subscene=159&subscene=&scenenote=https%3A%2F%2Fmp.weixin.qq.com%2Fs%2FUru6JnFjXBF6unDHZu3gYg#wechat_redirect (二維碼自動識別)
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的可以關注私信我哦~
歡迎小白、萌新、大佬加入我們
小白學習交流基地【(九七四)(七二四)(八九四)】
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/246424.html
標籤:其他
