您的位置:首页 > 编程语言 > Python开发

python实现生成二维码图片

2014-11-27 23:58 661 查看
python生成二维码:
qrcode库

pip install qrcode 或
easyinstall.exe qrcode

代码实现:
#!/usr/bin/python
#coding:utf-8
####
#author sevenqi
#date 20141125
#功能生成一个二维码图片,返回二维码图片的路径
#
####

import qrcode
import os
import sys
import hashlib
import time

CUR_PATH = os.path.dirname(os.path.abspath(__file__))
reload(sys)
sys.setdefaultencoding('utf8')

#get md5
def get_hashvalue(string):
m = hashlib.md5()
string = "%s%s" % (string, int(time.time()))
m.update(string)
hashValue = m.hexdigest()
return hashValue

##
#code_content 是二维码的内容
#savepath 是二维码保存的路径, 不指定默认保存在当前路径下
#
##
def generateQRCode(code_content, savepath = CUR_PATH, code_box_size = 30, code_border = 4, code_version = 1, code_error_correction = qrcode.constants.ERROR_CORRECT_L):
qr = qrcode.QRCode(
version=code_version,
error_correction=code_error_correction,
box_size=code_box_size,
border=code_border,
)

qr.add_data(code_content)
qr.make(fit=True)

img_object = qr.make_image()

savepath = savepath + "/" + get_hashvalue(code_content) + ".jpg"

img_savefile = open(savepath, "wb")
img_object.save(img_savefile, "JPEG")
img_savefile.close()

return savepath

if __name__ == "__main__":
print generateQRCode("http://www.baidu.com")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: