转载自:http://dhq.me/python-qr-code-generator
二维码简称 QR Code(Quick Response Code),学名为快速响应矩阵码,是二维条码的一种,由日本的 Denso Wave 公司于 1994 年发明。现随着智能手机的普及,已广泛应用于平常生活中,例如商品信息查询、社交好友互动、网络地址访问等等。
安装 Python 的二维码库 -- qrcode
由于生成 qrcode 图片需要依赖 Python 的图像库,所以需要先安装 Python 图像库 PIL(Python Imaging Library),不然会遇到 "ImportError: No module named Image" 的错误:
1
|
sudo
easy_install pil
|
如果安装 pil 时出现以下错误:
1
2
3
4
5
|
_imagingft.c:73:10: fatal error: 'freetype/fterrors.h' file not found
#include <freetype/fterrors.h>
^
1 error generated.
error: Setup script exited with error: command 'cc' failed with exit status 1
|
在 StackOverflow 上发现是 Mac 下所依赖的 FreeType 链接变更问题,解决如下:
1
2
|
ln
-s
/usr/local/include/freetype2
/usr/local/include/freetype
sudo
easy_install -U pil
|
安装 qrcode 库:
1
|
sudo
easy_install qrcode
|
成功安装后,即可以在终端里使用 qr 命令生成二维码了:
1
|
qr
"Just a test"
>
test
.png
|
1
|
qr --help
|
示例代码
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import
qrcode
qr
=
qrcode.QRCode(
version
=
2
,
error_correction
=
qrcode.constants.ERROR_CORRECT_L,
box_size
=
10
,
border
=
1
)
qr.make(fit
=
True
)
img
=
qr.make_image()
img.save(
"dhqme_qrcode.png"
)
|
参数 version 表示生成二维码的尺寸大小,取值范围是 1 至 40,最小尺寸 1 会生成 21 * 21 的二维码,version 每增加 1,生成的二维码就会添加 4 尺寸,例如 version 是 2,则生成 25 * 25 的二维码。
参数 error_correction 指定二维码的容错系数,分别有以下4个系数:
- ERROR_CORRECT_L: 7%的字码可被容错
- ERROR_CORRECT_M: 15%的字码可被容错
- ERROR_CORRECT_Q: 25%的字码可被容错
- ERROR_CORRECT_H: 30%的字码可被容错
参数 box_size 表示二维码里每个格子的像素大小。
参数 border 表示边框的格子厚度是多少(默认是4)。
运行上面代码会生成敝站的一个 QR Code:
生成带有图标的二维码
二维码的容错系数(上面所指的 error_correction)越高,生成的二维码则可允许的残缺率越大,且二维码的数据主要保存在图片的四个角上,所以在二维码中间放一个小图标,对二维码的识别也是不受多大影响的。
对于插入在二维码上的图标大小,这里指定限制图标的大小尺寸最大是二维码长宽的 1/4,以免残缺太大,影响识别。
最后结合 Python 图像库(PIL)的操作,把图片黏贴(paste)在二维码图片的中间,便可以生成一个带有图标的二维码,具体操作代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
import
Image
import
qrcode
qr
=
qrcode.QRCode(
version
=
2
,
error_correction
=
qrcode.constants.ERROR_CORRECT_H,
box_size
=
10
,
border
=
1
)
qr.make(fit
=
True
)
img
=
qr.make_image()
img
=
img.convert(
"RGBA"
)
icon
=
Image.
open
(
"favicon.png"
)
img_w, img_h
=
img.size
factor
=
4
size_w
=
int
(img_w
/
factor)
size_h
=
int
(img_h
/
factor)
icon_w, icon_h
=
icon.size
if
icon_w > size_w:
icon_w
=
size_w
if
icon_h > size_h:
icon_h
=
size_h
icon
=
icon.resize((icon_w, icon_h), Image.ANTIALIAS)
w
=
int
((img_w
-
icon_w)
/
2
)
h
=
int
((img_h
-
icon_h)
/
2
)
img.paste(icon, (w, h), icon)
img.save(
"dhqme_qrcode.png"
)
|