最近有个需求:批量生成带Logo的二维码
生成二维码比较简单,网上的资源也比较多,不赘述了。自己研究了一下加了logo并且美化了一下(网上的资源直接加Logo特别丑!!!忍不了!!!),直接上代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
def create_qrcode(url, filename):
qr = qrcode.QRCode(
version = 1 ,
#设置容错率为最高
error_correction = qrcode.ERROR_CORRECT_H,
box_size = 10 ,
border = 4 ,
)
qr.add_data(url)
qr.make(fit = True )
img = qr.make_image()
#设置二维码为彩色
img = img.convert( "RGBA" )
icon = Image. open ( '/Users/Jason/Desktop/qrcode/logo.png' )
w, h = img.size
factor = 4
size_w = int (w / factor)
size_h = int (h / factor)
icon_w, icon_h = icon.size
if icon_w > size_w:
icon_w = size_w
if icon_h > size_h:
icon_h = size_h
icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)
w = int ((w - icon_w) / 2 )
h = int ((h - icon_h) / 2 )
icon = icon.convert( "RGBA" )
newimg = Image.new( "RGBA" , (icon_w + 8 , icon_h + 8 ), ( 255 , 255 , 255 ))
img.paste(newimg, (w - 4 , h - 4 ), newimg)
img.paste(icon, (w, h), icon)
img.save( '/Users/Jason/Desktop/qrcode/' + filename + '.png' , quality = 100 )
|
以上这篇Python3批量生成带logo的二维码方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/jason_src/article/details/69523438