python的優越之處就在于他可以直接調用已經封裝好的包
首先,下載pillow
和qrcode
包 終端下鍵入一下命令:
pip3 install pillow #python2 用pip install pillowpip3 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
生成的二維碼
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對武林站長站的支持。如果你想了解更多相關內容請查看下面相關鏈接
新聞熱點
疑難解答