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   星河赵  阅读(1305)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
历史上的今天:
2018-06-27 django model form 保存方法 django-rest-framework save 修改某一项值 方法
2018-06-27 Django 查询时间段 时间搜索 过滤

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示