python之qrcode模块生成二维码 终端生成二维码

一、准备安装包

  • 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

 

三、终端生成二维码

 

import qrcode

qr = qrcode.QRCode()
qr.add_data("http://www.baidu.com")
#invert=True白底黑块,有些app不识别黑底白块.
qr.print_ascii()
#qr.print_ascii(invert=True)

 

 

 

 

 




 

posted on 2022-06-27 11:27  星河赵  阅读(1439)  评论(0)    收藏  举报

导航