一、准备安装包
- pip install qrcode
qrcode 依赖 Image 这个包:
- pip install Image
二、调试代码
import qrcode //模块导入 //调用qrcode的make()方法传入url或者想要展示的内容 img = qrcode.make('http://www.baidu.com') //写入文件 with open('test.png', 'wb') as f: img.save(f)
生成的二维码:
将二维码转为byte 类型
# -*- coding: utf8 -*- """ 二维码生成器 """ from io import BytesIO import qrcode # 生成二维码 img = qrcode.make(data="你好") print img print type(img) stream = BytesIO() img.save(stream, 'jpeg') print stream.getvalue()
flask response 直接返回图片
if pay_type == "native": stream = BytesIO() img = qrcode.make(data=client_payment_args["code_url"]) img.save(stream, 'jpeg') response = make_response(stream.getvalue()) response.headers['Content-Type'] = 'image/jpeg' return response