Python 的生成二维码生成库 -- qrcode

转载自: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.add_data( "http://dhq.me/" )
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:

dhq.me 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.add_data( "http://dhq.me/" )
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)
 
= int ((img_w  - icon_w)  / 2 )
= int ((img_h  - icon_h)  / 2 )
img.paste(icon, (w, h), icon)
 
img.save( "dhqme_qrcode.png" )

dhq.me QR Code Logo

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值