当前位置:主页 > 软件编程 > Python代码 >

Python用5行代码写一个自定义简单二维码

时间:2021-07-20 08:55:03 | 栏目:Python代码 | 点击:

python的优越之处就在于他可以直接调用已经封装好的包

首先,下载pillowqrcode包  终端下键入一下命令:

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

生成的二维码

总结

您可能感兴趣的文章:

相关文章