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

yipeiwu_com6年前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

生成的二维码

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对【听图阁-专注于Python设计】的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

Python中shapefile转换geojson的示例

shapefile转换geojson import shapefile import codecs from json import dumps # read the shapefi...

Django URL传递参数的方法总结

1 无参数情况 配置URL及其视图如下: (r'^hello/$', hello) def hello(request): return HttpResponse("Hell...

python 字典套字典或列表的示例

文件f1 A 1 a A 1 b A...

Python 堆叠柱状图绘制方法

Python 堆叠柱状图绘制方法

本文介绍了Python 堆叠柱状图绘制方法,分享给大家,具体如下: ''''''''''''''''''''''''''''''''''''''''''''''''''''''''...

pycharm 将python文件打包为exe格式的方法

pycharm 将python文件打包为exe格式的方法

因为近期正在学习python,就需要将python文件打包为exe可执行文件,就将该过程记录下来。 首先我是通过Pyinstall打包的,具体安装及打包步骤如下 1.打开终端控制台 通过...