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

5行代码写一个自定义简单二维码——python

2018-02-05 16:15 1046 查看
python的优越之处就在于他可以直接调用已经封装好的包

首先——下载pillow和qrcode包  终端下键入一下命令:

pip3 install pillow   #python2 用pip install pillow

pip3 install qrcode

import qrcode

# 定义一个类名
def qrcodeWithUrl(url):

img = qrcode.make(url)      # 生成一个二维码
savePath = "baidu.png"      # 存储二维码 命名
img.save(savePath)          # 保存二维码

def qrcodeWithText(text):
img = qrcode.make(text)
savePath = "2.png"
img.save(savePath)

content = input("请输入一句话或者键入一个网址")
if "http" in content:           # 如果是网址 则运行 qrcodeWithUrl(url):
qrcodeWithUrl(content)
else:                           # 如果是文本 则运行 qrcodeWithText(text):
qrcodeWithText(content)

print("二维码已经生成好")



运行代码:

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/apple/test1/fork/demo3.py
请输入一句话http://www.redporn.com
二位嘛已经生成好

Process finished with exit code 0


生成二维码

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: