Python 生成带Logo的圆角带边框二维码

Python 生成二维码方式就不累述了,不会的自己百度吧

但python生成的二维码太难看了,要么没有logo,要么logo直接贴进去的,难看死了,有的也处理了一下,但没有圆角,也难看:

以下:是不是觉得后边几个好看!!!

 

直接上代码:

 

 1 # coding = utf-8
 2 # 二维码生成
 3 
 4 import qrcode
 5 from PIL import Image, ImageDraw
 6 
 7 
 8 # 设置圆角
 9 def circle_crop_image(im, radii):
10     # 创建一个黑色背景的画布
11     circle = Image.new('L', (radii * 2, radii * 2), 0)
12     # 画黑色方形
13     draw = ImageDraw.Draw(circle)
14     # 画白色圆形
15     draw.ellipse((0, 0, radii * 2, radii * 2), fill=255)
16     # 把原图转换成RGBA模式,增加alpha通道
17     img = im.convert("RGBA")
18     w, h = img.size
19     # 画4个角(将整圆分离为4个部分)再粘贴到alpha通道
20     alpha = Image.new('L', img.size, 255)
21     # 左上角
22     alpha.paste(circle.crop((0, 0, radii, radii)), (0, 0))
23     # 右上角
24     alpha.paste(circle.crop((radii, 0, radii * 2, radii)), (w - radii, 0))
25     # 右下角
26     alpha.paste(circle.crop((radii, radii, radii * 2, radii * 2)), (w - radii, h - radii))
27     # 左下角
28     alpha.paste(circle.crop((0, radii, radii, radii * 2)), (0, h - radii))
29     # 白色区域透明可见,黑色区域不可见
30     img.putalpha(alpha)
31     return img
32 
33 
34 # 生成二位码
35 def create_qr_code(url, icon_file, file_name, save_path):
36     qr = qrcode.QRCode(
37         version=5,
38         # 设置容错率为最高
39         error_correction=qrcode.ERROR_CORRECT_H,
40         box_size=5,
41         border=1,
42     )
43     qr.add_data(url)
44     qr.make(fit=True)
45     img = qr.make_image()
46     img = img.convert("RGBA")
47     icon = Image.open(icon_file)
48     # 把RGB的图转换成RGBA模式,处理alpha透明通道(后边替换透明为白色)
49     icon = icon.convert("RGBA")
50     w, h = img.size
51     icon_w, icon_h = icon.size
52     # 超过80的压缩到80
53     if icon_w > 80:
54         icon = icon.resize((80, 80), Image.ANTIALIAS)
55         icon_w, icon_h = icon.size
56         w = int((w - 80) / 2)
57         h = int((h - 80) / 2)
58     else:
59         w = int((w - icon_w) / 2)
60         h = int((h - icon_h) / 2)
61     # 把png背景色转换为白色,避免处理裁剪圆角时出现黑边
62     w_d = Image.new('RGBA', icon.size, (255, 255, 255))
63     w_d.paste(icon, (0, 0, icon_w, icon_h), icon)
64     # r = icon_w // 15
65     r = 6
66     icon = circle_crop_image(w_d, r)
67     # 白底图
68     white_img = Image.new("RGBA", (icon_w + 6, icon_h + 6), (255, 255, 255))
69     white_img = circle_crop_image(white_img, r)
70     # 灰底图
71     gray_img = Image.new("RGBA", (icon_w + 2, icon_h + 2), (230, 230, 230))
72     # 灰底图圆角处理
73     gray_img = circle_crop_image(gray_img, r)
74     # 粘贴灰底图
75     white_img.paste(gray_img, (2, 2), gray_img)
76     # 粘贴白图
77     img.paste(white_img, (w - 2, h - 2), white_img)
78     # 粘贴icon
79     img.paste(icon, (w + 1, h + 1), icon)
80     save_file = save_path + file_name + '.png'
81     img.save(save_file, quality=100)
82     # img.show()
83     return save_file
84 
85 
86 if __name__ == '__main__':
87 
88     icon_path = '/Users/drew/.jenkins/workspace/Android/app/src/main/res/mipmap-xxhdpi/ic_collect_icon.png'
89     save_path = '/Users/Work/PycharmProjects/FM_Build_Packaging/resources/img/qrcode'
90     create_qr_code('http://www.thecover.cn', icon_path, 'cs', 'save_path')

 

以上,都有注释。

另外说明一下,PIL在python3.x版本需要安装的是pillow。

遇到的坑:

Image.open('file\\img\\80.png')  这个地方打开图片时候报错:

raise OSError(message + " when reading image file")
OSError: broken data stream when reading image file

加了以下代码:

from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

 

不报错了,但是一直没法把icon粘贴过去,后来才知道,icon没有读取出来,原图有问题,图片估计是损坏了,换个图就ok了!

所以,不要以为代码不报错了,就没问题了,哎,坑~~~

再者,我们APP的icon都是.png,模式都是RGB,都有透明背景,无论是压缩后裁剪还是裁剪后压缩,有的APP的logo都会出现黑色边框:

这个问题困扰了差不多半个月,找了各种方法都没解决掉,偶然间看到有人把透明背景替换成白色,突然间就找到怎么解决黑边的问题:

为什么不把图片的透明层直接替换成白色呢~~哈哈哈~~

替换成白色背景时候原图一定要转换成RGBA,用mask覆盖就行。

有遇到坑的欢迎留言交流。

 
posted @ 2020-04-23 17:02  drewgg  阅读(1081)  评论(0编辑  收藏  举报