欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python第三方库qrcode或MyQr生成博客地址二维码

程序员文章站 2022-03-10 09:52:42
目录myqr前期准备qrcode下载地址:qrcode · pypiqr code image generator使用简介:import qrcodeqr = qrcode.qrcode( ve...

前期准备

qrcode

下载地址:

qrcode · pypi

qr code image generator

使用简介:

import qrcode
qr = qrcode.qrcode(
    version=5,
    error_correction=qrcode.constants.error_correct_l,
    box_size=10,
    border=4,
)
qr.add_data('https://blog.csdn.net/boysoft2002/article/details/120213715')
qr.make(fit=true)
img = qr.make_image(fill_color="black", back_color="white")

myqr

下载地址:

myqr · pypi

https://pypi.org/project/myqr/

使用简介:

from myqr import myqr
myqr.run(words = 'https://blog.csdn.net/boysoft2002/article/details/120257133',
         save_name = 'qrcode.jpg'
         ) 
#生成带背景图片的二维码
myqr.run(words = 'https://blog.csdn.net/boysoft2002/article/details/120242318',
         picture = r'background.jpg',  #支持动态gif作背景
         colorized = true, # true:彩色,false:黑白
         save_name = 'qrcode.png'
         )

安装第三方库

如下二选一安装都可以,若存在其它依赖库请一并安装:

d:\>pip install qrcode
collecting qrcode
  downloading qrcode-7.3.tar.gz (43 kb)
     |████████████████████████████████| 43 kb 191 kb/s
collecting colorama
  using cached colorama-0.4.4-py2.py3-none-any.whl (16 kb)
using legacy 'setup.py install' for qrcode, since package 'wheel' is not installed.
installing collected packages: colorama, qrcode
    running setup.py install for qrcode ... done
successfully installed colorama-0.4.4 qrcode-7.3
d:\>pip install myqr
collecting myqr
  downloading myqr-2.3.1.zip (16 kb)
collecting imageio>=1.5
  downloading imageio-2.9.0-py3-none-any.whl (3.3 mb)
     |████████████████████████████████| 3.3 mb 3.2 mb/s
requirement already satisfied: numpy>=1.11.1 in d:\python\lib\site-packages (from myqr) (1.20.2)
requirement already satisfied: pillow>=3.3.1 in d:\python\lib\site-packages (from myqr) (8.2.0)
using legacy 'setup.py install' for myqr, since package 'wheel' is not installed.
installing collected packages: imageio, myqr
    running setup.py install for myqr ... done
successfully installed myqr-2.3.1 imageio-2.9.0

代码

以qrcode为例,快速生成博客的二维码(粘贴自己的网址到urls变量中,文章标题非必须项)。

urls = '''
python 斐波那契数列递归的改进,算第1000万项只要4秒钟!
https://blog.csdn.net/boysoft2002/article/details/120257133
python 不自己试试,还真猜不出递归函数的时间复杂度!
https://blog.csdn.net/boysoft2002/article/details/120242318
python 算法的时间复杂度和空间复杂度 (实例解析)
https://blog.csdn.net/boysoft2002/article/details/120213715
python 控制台操作的文字版“数独”游戏(非gui版本)
https://blog.csdn.net/boysoft2002/article/details/120202704
'''
import re,os,qrcode
import tkinter as tk
url = re.findall(r'https://.*[0-9]', urls)
for i,u in enumerate(url):
    if u.find('?')!=-1:
        url[i] = u[:u.find('?')]
        #print(u)
imgs,index = [],0
for u in url:
    img = qrcode.make(u)
    id = u[::-1]
    id = id[:id.find('/')][::-1]
    imgs.append('qrcode'+id+'.png')
    with open(imgs[-1],'wb') as f:
        img.save(f)
def nextqr():
    global imgs,index,png,qrc
    index += 1
    if index==len(imgs):index = 0
    btn['text']=f'no:{index+1}: {imgs[index]}'
    png = tk.photoimage(file=imgs[index])
    qrc = cv.create_image(205,205,image=png)
    cv.update() 
def on_close():
    global imgs
    p = os.getcwd()
    if p[-1]!='\\':
        p += '\\'
    for f in imgs:
        os.remove(p+f)
    root.destroy()   
root = tk.tk()
root.geometry('600x520')
root.title('qrcode')
cv = tk.canvas(root, width=410, height=410)
cv.pack()
png = tk.photoimage(file=imgs[0])
qrc = cv.create_image(205,205,image=png)
btn = tk.button(root,text=f'no:1: {imgs[0]}',command=nextqr,fg='red')
btn.place(x=220,y=450)
root.protocol("wm_delete_window", on_close)
root.mainloop()

运行效果

点击 button 按钮逐个展示二维码:

Python第三方库qrcode或MyQr生成博客地址二维码

我的使用环境:windows7 + python3.8.8 

 --all done!

以上就是python第三方库qrcode或myqr生成博客地址二维码的详细内容,更多关于python第三方库生成博客地址二维码的资料请关注其它相关文章!