Python 文字轉 QR Code

本篇 ShengYu 介紹 Python 文字轉 QR Code 的方法。

最基本最簡單產生 QR Code 的方式如下,先安裝好 Python 的 qrcode 套件,

1
pip3 install qrcode

安裝好 Python 的 qrcode 套件後,將 hello world 這個字串轉成 QR Code 看看,程式碼如下,

python3-qrcode.py
1
2
3
4
5
6
7
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import qrcode

img = qrcode.make('hello world')
img.save("qrcode.png")
img.show()

hello world 這個字串轉成的 QR Code 圖片如下所示,用任意的 QR Code 掃描 App 應該都可以掃描的出來這段文字,

文字轉 QR Code 小工具

我將上述這功能整理成一個小工具,變成執行程式時帶入任意字串,便能將該字串轉換成 QR Code 圖檔並顯示跟存檔,

python3-text2qrcode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import qrcode

if __name__ == '__main__':
if len(sys.argv) < 2:
print('no argument')
sys.exit()
s = sys.argv[1]
img = qrcode.make(s)
print('convert ' + s + ' to qrcode')
img.show()
print('save qrcode to qrcode.png')
img.save("qrcode.png")

以上就是本篇的 Python 文字轉 QR Code 教學。

其它相關文章:Python QR Code 轉文字