Python-QRCode模块,生成二维码
2016-07-19 本文已影响102人
望月成三人
Usage
From the command line, use the installed qr
script:
qr "Some text" > test.png
Or in Python, use the make
shortcut function:
import qrcodeimg = qrcode.make('Some data here')
Advanced Usage
For more control, use the QRCode
class. For example:
import qrcode
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data('Some data')
qr.make(fit=True)
img = qr.make_image()
解释一下:
- version:一个整数,范围为1到40,表示二维码的大小(最小值是1,是个12×12的矩阵),如果想让程序自动生成,将值设置为 None 并使用 fit=True 参数即可。
- error_correction:二维码的纠错范围,可以选择4个常量
- ERROR_CORRECT_L 7%以下的错误会被纠正
- ERROR_CORRECT_M (default) 15%以下的错误会被纠正
- ERROR_CORRECT_Q 25 %以下的错误会被纠正
- ERROR_CORRECT_H. 30%以下的错误会被纠正
- boxsize:每个点(方块)中的像素个数
- border:二维码距图像外围边框距离,默认为4,而且相关规定最小为4