pylhb4qrcode 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
Non-Commercial Use License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Mr.Lee
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software for non-commercial purposes only, including without limitation
|
|
8
|
+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
9
|
+
and/or sell copies of the Software, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
1. The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
2. Commercial use of the Software is expressly prohibited without prior written
|
|
15
|
+
permission from the copyright holder. For purposes of this license, commercial
|
|
16
|
+
use means any use of the Software that is intended for or directed toward
|
|
17
|
+
commercial advantage or monetary compensation.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: pylhb4qrcode
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Mr.Lee's QRCode Helpers
|
|
5
|
+
Author: SoftGod4MrLi
|
|
6
|
+
Author-email: SoftGod4MrLi <596928288@qq.com>
|
|
7
|
+
License: Non-Commercial Use License
|
|
8
|
+
|
|
9
|
+
Copyright (c) 2025 Mr.Lee
|
|
10
|
+
|
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
12
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
13
|
+
in the Software for non-commercial purposes only, including without limitation
|
|
14
|
+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
15
|
+
and/or sell copies of the Software, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
1. The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
2. Commercial use of the Software is expressly prohibited without prior written
|
|
21
|
+
permission from the copyright holder. For purposes of this license, commercial
|
|
22
|
+
use means any use of the Software that is intended for or directed toward
|
|
23
|
+
commercial advantage or monetary compensation.
|
|
24
|
+
|
|
25
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
26
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
27
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
28
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
29
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
30
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
31
|
+
SOFTWARE.
|
|
32
|
+
Requires-Dist: pillow>=12.2.0
|
|
33
|
+
Requires-Dist: qrcode>=8.2
|
|
34
|
+
Requires-Python: >=3.12
|
|
35
|
+
Project-URL: Homepage, https://github.com/SoftGod4MrLi/pylhb4qrcode
|
|
36
|
+
Project-URL: Documentation, https://github.com/SoftGod4MrLi/pylhb4qrcode
|
|
37
|
+
Project-URL: Repository, https://github.com/SoftGod4MrLi/pylhb4qrcode
|
|
38
|
+
Project-URL: Issues, https://github.com/SoftGod4MrLi/pylhb4qrcode/issues
|
|
39
|
+
Description-Content-Type: text/markdown
|
|
40
|
+
|
|
41
|
+
# pylhb4qrcode
|
|
42
|
+
|
|
43
|
+
[](https://pypi.org/project/pylhb-qrcode/)
|
|
44
|
+
[](https://github.com/SoftGod4MrLi/pylhb-qrcode)
|
|
45
|
+
[](https://github.com/SoftGod4MrLi/pylhb-qrcode/blob/main/LICENSE)
|
|
46
|
+

|
|
47
|
+
[](https://github.com/SoftGod4MrLi/pylhb-qrcode)
|
|
48
|
+
|
|
49
|
+
pylhb-qrcode 是我在工作过程中陆续整理的一个 Python qrcode 工具包,里面就放在一个关于qrcode操作的类及相关函数。与其说是一个正式的开源项目,不如说是我自己的“代码杂物间”——只不过把它打包了一下,方便在不同项目之间复用。
|
|
50
|
+
|
|
51
|
+
> 由于是个人使用为主,很多设计可能带着比较强的个人习惯,也未必是最优解。如果您发现了问题或有更好的建议,非常欢迎指正。
|
|
52
|
+
|
|
53
|
+
## 安装
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
pip install pylhb4qrcode
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## 命令
|
|
60
|
+
|
|
61
|
+
- 创建二维码
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
pylhb4qrcode --str 596928288
|
|
65
|
+
pylhb4qrcode --str 596928288 --desc 扫我加QQ --logo my.png --circular --file MyQQ.png
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## 🌺myqrcode模块
|
|
69
|
+
|
|
70
|
+
二维码生成,并可以在二维码下方添加描述文字。
|
|
71
|
+
|
|
72
|
+
使用示例:
|
|
73
|
+
|
|
74
|
+
```
|
|
75
|
+
from pylhb4qrcode.myqrcode import MyQRCode
|
|
76
|
+
|
|
77
|
+
if __name__ == "__main__":
|
|
78
|
+
qr=MyQRCode()
|
|
79
|
+
qr.get(qrStr:"596928288",qrDesc="扫我加QQ")
|
|
80
|
+
```
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# pylhb4qrcode
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/pylhb-qrcode/)
|
|
4
|
+
[](https://github.com/SoftGod4MrLi/pylhb-qrcode)
|
|
5
|
+
[](https://github.com/SoftGod4MrLi/pylhb-qrcode/blob/main/LICENSE)
|
|
6
|
+

|
|
7
|
+
[](https://github.com/SoftGod4MrLi/pylhb-qrcode)
|
|
8
|
+
|
|
9
|
+
pylhb-qrcode 是我在工作过程中陆续整理的一个 Python qrcode 工具包,里面就放在一个关于qrcode操作的类及相关函数。与其说是一个正式的开源项目,不如说是我自己的“代码杂物间”——只不过把它打包了一下,方便在不同项目之间复用。
|
|
10
|
+
|
|
11
|
+
> 由于是个人使用为主,很多设计可能带着比较强的个人习惯,也未必是最优解。如果您发现了问题或有更好的建议,非常欢迎指正。
|
|
12
|
+
|
|
13
|
+
## 安装
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
pip install pylhb4qrcode
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## 命令
|
|
20
|
+
|
|
21
|
+
- 创建二维码
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
pylhb4qrcode --str 596928288
|
|
25
|
+
pylhb4qrcode --str 596928288 --desc 扫我加QQ --logo my.png --circular --file MyQQ.png
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## 🌺myqrcode模块
|
|
29
|
+
|
|
30
|
+
二维码生成,并可以在二维码下方添加描述文字。
|
|
31
|
+
|
|
32
|
+
使用示例:
|
|
33
|
+
|
|
34
|
+
```
|
|
35
|
+
from pylhb4qrcode.myqrcode import MyQRCode
|
|
36
|
+
|
|
37
|
+
if __name__ == "__main__":
|
|
38
|
+
qr=MyQRCode()
|
|
39
|
+
qr.get(qrStr:"596928288",qrDesc="扫我加QQ")
|
|
40
|
+
```
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "pylhb4qrcode"
|
|
3
|
+
version = "1.0.0"
|
|
4
|
+
description = "Mr.Lee's QRCode Helpers"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = {file = "LICENSE"}
|
|
7
|
+
authors = [
|
|
8
|
+
{ name = "SoftGod4MrLi", email = "596928288@qq.com" }
|
|
9
|
+
]
|
|
10
|
+
requires-python = ">=3.12"
|
|
11
|
+
dependencies = [
|
|
12
|
+
"pillow>=12.2.0",
|
|
13
|
+
"qrcode>=8.2",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
[project.urls]
|
|
17
|
+
Homepage = "https://github.com/SoftGod4MrLi/pylhb4qrcode"
|
|
18
|
+
Documentation = "https://github.com/SoftGod4MrLi/pylhb4qrcode"
|
|
19
|
+
Repository = "https://github.com/SoftGod4MrLi/pylhb4qrcode"
|
|
20
|
+
Issues = "https://github.com/SoftGod4MrLi/pylhb4qrcode/issues"
|
|
21
|
+
|
|
22
|
+
[project.scripts]
|
|
23
|
+
pylhb-qrcode = "pylhb4qrcode:main"
|
|
24
|
+
|
|
25
|
+
[build-system]
|
|
26
|
+
requires = ["uv_build>=0.11.18,<1000.12.0"]
|
|
27
|
+
build-backend = "uv_build"
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
# Mr.Lee's Module
|
|
3
|
+
from .myqrcode import MyQRCode
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
"MyQRCode"
|
|
7
|
+
]
|
|
8
|
+
|
|
9
|
+
def main() -> None:
|
|
10
|
+
parser = argparse.ArgumentParser(description="Mr.Lee's QRCode Helpers")
|
|
11
|
+
parser.add_argument('-v', '--version', action='store_true', help='show version')
|
|
12
|
+
parser.add_argument("-s", "--str", default=None, type=str, help="二维码字符")
|
|
13
|
+
parser.add_argument("-d", "--desc", default=None, type=str, help="二维码描述")
|
|
14
|
+
parser.add_argument("-l", "--logo", default=None, type=str, help="二维码Logo")
|
|
15
|
+
parser.add_argument("-f", "--file", default="QR.png", type=str, help="二维码文件名")
|
|
16
|
+
parser.add_argument('-c', '--circular', action='store_true', help='圆形Logo')
|
|
17
|
+
args = parser.parse_args()
|
|
18
|
+
|
|
19
|
+
# show version
|
|
20
|
+
if args.version:
|
|
21
|
+
try:
|
|
22
|
+
from importlib.metadata import version, PackageNotFoundError
|
|
23
|
+
# 直接从包元数据获取版本号
|
|
24
|
+
ver = version("pylhb-yaml")
|
|
25
|
+
print(f"pylhb-yaml {ver}")
|
|
26
|
+
except PackageNotFoundError:
|
|
27
|
+
print("Package not found.")
|
|
28
|
+
except ImportError:
|
|
29
|
+
print("Import errored.")
|
|
30
|
+
except:
|
|
31
|
+
print("Other errored.")
|
|
32
|
+
return
|
|
33
|
+
|
|
34
|
+
# 生成二维码
|
|
35
|
+
if args.qrstr:
|
|
36
|
+
qr=MyQRCode()
|
|
37
|
+
if args.logo:
|
|
38
|
+
if args.circular:
|
|
39
|
+
qrImg=qr.getWithLogo4Circular(args.str,args.desc,args.logo)
|
|
40
|
+
qrImg.save(args.file)
|
|
41
|
+
else:
|
|
42
|
+
qrImg=qr.getWithLogo(args.str,args.desc,args.logo)
|
|
43
|
+
qrImg.save(args.file)
|
|
44
|
+
else:
|
|
45
|
+
qrImg=qr.get(args.str,args.desc)
|
|
46
|
+
qrImg.save(args.file)
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
"""
|
|
2
|
+
模块:myqrcode
|
|
3
|
+
作者:李生
|
|
4
|
+
描述:二维码生成
|
|
5
|
+
"""
|
|
6
|
+
import qrcode
|
|
7
|
+
from PIL import Image,ImageFont,ImageDraw
|
|
8
|
+
import os
|
|
9
|
+
|
|
10
|
+
class MyQRCode:
|
|
11
|
+
def __init__(self,qrVersion=1,qrBoxSize = 10,qrBorder = 5,fontColor="black",backColor="white",marginTop=20,marginButtom=20,fontSize=16) -> None:
|
|
12
|
+
"""
|
|
13
|
+
构造函数
|
|
14
|
+
Args:
|
|
15
|
+
qrVersion:二维码的规格,即控制二维码包含多少行和列,有效值为1-40。
|
|
16
|
+
qrBoxSize:每个格子的像素大小
|
|
17
|
+
qrBorder:边框的厚度
|
|
18
|
+
fontColor:前景色
|
|
19
|
+
backColor:背景色
|
|
20
|
+
marginTop:描述文字上边距
|
|
21
|
+
marginButtom:描述文字下边距
|
|
22
|
+
fontSize:描述文字体号
|
|
23
|
+
"""
|
|
24
|
+
self.qrVersion=qrVersion
|
|
25
|
+
self.qrBoxSize=qrBoxSize
|
|
26
|
+
self.qrBorder=qrBorder
|
|
27
|
+
self.fontColor=fontColor
|
|
28
|
+
self.backColor=backColor
|
|
29
|
+
self.marginTop=marginTop
|
|
30
|
+
self.marginButtom=marginButtom
|
|
31
|
+
self.fontSize=fontSize
|
|
32
|
+
|
|
33
|
+
def addText(self,qrImg,qrDesc):
|
|
34
|
+
"""
|
|
35
|
+
在二维码图片下方添加文本,生成新图片
|
|
36
|
+
Args:
|
|
37
|
+
qrImg:二维码图片
|
|
38
|
+
text:文字
|
|
39
|
+
Returns:
|
|
40
|
+
Image
|
|
41
|
+
"""
|
|
42
|
+
# 获取二维码尺寸
|
|
43
|
+
qrWidth, qrHeight = qrImg.size
|
|
44
|
+
|
|
45
|
+
# 设置字体(使用默认字体,可根据需要修改路径)
|
|
46
|
+
try:
|
|
47
|
+
# 尝试使用系统字体,Windows可用arial.ttf,macOS/Linux可用DejaVuSans.ttf
|
|
48
|
+
font = ImageFont.truetype("C:/Windows/Fonts/msyh.ttc", self.fontSize)
|
|
49
|
+
except:
|
|
50
|
+
try:
|
|
51
|
+
font = ImageFont.truetype("C:/Windows/Fonts/simhei.ttf", self.fontSize)
|
|
52
|
+
except:
|
|
53
|
+
font = ImageFont.load_default()
|
|
54
|
+
|
|
55
|
+
# 计算文本尺寸
|
|
56
|
+
drawTemp = ImageDraw.Draw(Image.new('RGB', (1, 1)))
|
|
57
|
+
bbox = drawTemp.textbbox((0, 0), text, font=font)
|
|
58
|
+
textWidth = bbox[2] - bbox[0]
|
|
59
|
+
textHeight = bbox[3] - bbox[1]
|
|
60
|
+
|
|
61
|
+
# 新图片高度 = 二维码高度 + 间距 + 文本高度
|
|
62
|
+
newHeight = qrHeight + self.marginTop + textHeight+self.marginButtom
|
|
63
|
+
newWidth = max(qrWidth, textWidth) # 新图片宽度取二维码和文本中较宽者
|
|
64
|
+
|
|
65
|
+
# 创建新画布
|
|
66
|
+
newImg = Image.new('RGB', (newWidth, newHeight), self.backColor)
|
|
67
|
+
|
|
68
|
+
# 将二维码粘贴到新图片顶部(居中)
|
|
69
|
+
qrX = (newWidth - qrWidth) // 2
|
|
70
|
+
newImg.paste(qrImg, (qrX, 0))
|
|
71
|
+
|
|
72
|
+
# 绘制文本
|
|
73
|
+
draw = ImageDraw.Draw(newImg)
|
|
74
|
+
textX = (newWidth - textWidth) // 2
|
|
75
|
+
textY = qrHeight + self.marginTop
|
|
76
|
+
draw.text((textX, textY), qrDesc, fill=self.fontColor, font=font)
|
|
77
|
+
|
|
78
|
+
return newImg
|
|
79
|
+
|
|
80
|
+
def get(self,qrStr,qrDesc=None):
|
|
81
|
+
"""
|
|
82
|
+
生成简单二维码
|
|
83
|
+
Args:
|
|
84
|
+
qrStr:二维码字符
|
|
85
|
+
qrDesc:二维码描述
|
|
86
|
+
Returns:
|
|
87
|
+
Image
|
|
88
|
+
"""
|
|
89
|
+
qr=qrcode.QRCode(
|
|
90
|
+
version=self.qrVersion,
|
|
91
|
+
box_size=self.qrBoxSize,
|
|
92
|
+
border=self.qrBorder
|
|
93
|
+
)
|
|
94
|
+
qr.add_data(qrStr)
|
|
95
|
+
qr.make(fit=True)
|
|
96
|
+
qrImg=qr.make_image(fill_color=self.fontColor,back_color=self.backColor).convert('RGB')
|
|
97
|
+
if qrDesc:
|
|
98
|
+
return self.addText(qrImg,qrDesc,self.marginTop,self.marginButtom,self.fontSize,self.fontColor,self.backColor)
|
|
99
|
+
else:
|
|
100
|
+
return qrImg
|
|
101
|
+
|
|
102
|
+
def get4Easy(self,qrStr,qrDesc=None):
|
|
103
|
+
"""
|
|
104
|
+
生成简单二维码
|
|
105
|
+
Args:
|
|
106
|
+
qrStr:二维码字符
|
|
107
|
+
qrDesc:二维码描述
|
|
108
|
+
Returns:
|
|
109
|
+
Image
|
|
110
|
+
"""
|
|
111
|
+
qrImg=qrcode.make(qrStr).convert('RGB')
|
|
112
|
+
if qrDesc:
|
|
113
|
+
self.addText(qrImg,qrDesc,self.marginTop,self.marginButtom,self.fontSize,"black","white")
|
|
114
|
+
else:
|
|
115
|
+
return qrImg
|
|
116
|
+
|
|
117
|
+
def getWithLogo(self,qrStr,qrDesc=None,logoPath=None, logoRatio=0.2) -> None:
|
|
118
|
+
"""
|
|
119
|
+
生成带图标的二维码
|
|
120
|
+
Args:
|
|
121
|
+
qrStr:二维码字符
|
|
122
|
+
logoPath:Logo
|
|
123
|
+
qrDesc:二维码描述
|
|
124
|
+
logoRatio:Logo比率
|
|
125
|
+
Returns:
|
|
126
|
+
Image
|
|
127
|
+
"""
|
|
128
|
+
qr = qrcode.QRCode(
|
|
129
|
+
version=self.qrVersion,
|
|
130
|
+
error_correction=qrcode.constants.ERROR_CORRECT_H, # 使用高纠错级别
|
|
131
|
+
box_size=self.qrBoxSize,
|
|
132
|
+
border=self.qrBorder,
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
qr.add_data(qrStr)
|
|
136
|
+
qr.make(fit=True)
|
|
137
|
+
|
|
138
|
+
qrImg = qr.make_image(fill_color=self.fontColor, back_color=self.backColor).convert('RGB')
|
|
139
|
+
|
|
140
|
+
# 4. 如果有图标,添加到二维码中央
|
|
141
|
+
if logoPath and os.path.exists(logoPath):
|
|
142
|
+
# 加载图标
|
|
143
|
+
logo = Image.open(logoPath)
|
|
144
|
+
|
|
145
|
+
# 计算图标大小(二维码宽度的比例)
|
|
146
|
+
qrWidth, qrHeight = qrImg.size
|
|
147
|
+
logoSize = int(min(qrWidth, qrHeight) * logoRatio)
|
|
148
|
+
|
|
149
|
+
# 调整图标大小
|
|
150
|
+
logo = logo.resize((logoSize, logoSize), Image.Resampling.LANCZOS)
|
|
151
|
+
|
|
152
|
+
# 计算图标位置(居中)
|
|
153
|
+
logoPosition = ((qrWidth - logoSize) // 2, (qrHeight - logoSize) // 2)
|
|
154
|
+
|
|
155
|
+
# 创建白色背景圆角效果
|
|
156
|
+
mask = Image.new('RGBA', (logoSize, logoSize), (255, 255, 255, 0))
|
|
157
|
+
|
|
158
|
+
# 将图标粘贴到二维码上
|
|
159
|
+
if logo.mode == 'RGBA':
|
|
160
|
+
qrImg.paste(logo, logoPosition, logo)
|
|
161
|
+
else:
|
|
162
|
+
qrImg.paste(logo, logoPosition)
|
|
163
|
+
|
|
164
|
+
# 5. 返回
|
|
165
|
+
if qrDesc:
|
|
166
|
+
return self.addText(qrImg,qrDesc,self.marginTop,self.marginButtom,self.fontSize,self.fontColor,self.backColor)
|
|
167
|
+
else:
|
|
168
|
+
return qrImg
|
|
169
|
+
|
|
170
|
+
def getWithLogo4Circular(self,qrStr, qrDesc=None, logoPath=None, logoRatio=0.2):
|
|
171
|
+
"""
|
|
172
|
+
生成带圆形图标的二维码
|
|
173
|
+
Args:
|
|
174
|
+
qrStr:二维码字符
|
|
175
|
+
logoPath:Logo
|
|
176
|
+
qrDesc:二维码描述
|
|
177
|
+
logoRatio:Logo比率
|
|
178
|
+
Returns:
|
|
179
|
+
Image
|
|
180
|
+
"""
|
|
181
|
+
# 创建二维码
|
|
182
|
+
qr = qrcode.QRCode(
|
|
183
|
+
version=self.qrVersion,
|
|
184
|
+
error_correction=qrcode.constants.ERROR_CORRECT_H,
|
|
185
|
+
box_size=self.qrBoxSize,
|
|
186
|
+
border=self.qrBorder,
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
qr.add_data(qrStr)
|
|
190
|
+
qr.make(fit=True)
|
|
191
|
+
qrImg = qr.make_image(fill_color="black", back_color="white").convert('RGB')
|
|
192
|
+
|
|
193
|
+
if logoPath and os.path.exists(logoPath):
|
|
194
|
+
# 加载图标
|
|
195
|
+
logo = Image.open(logoPath)
|
|
196
|
+
qrWidth, qrHeight = qrImg.size
|
|
197
|
+
logoSize = int(min(qrWidth, qrHeight) * logoRatio)
|
|
198
|
+
logo = logo.resize((logoSize, logoSize), Image.Resampling.LANCZOS)
|
|
199
|
+
|
|
200
|
+
# 创建圆形遮罩
|
|
201
|
+
mask = Image.new('L', (logoSize, logoSize), 0)
|
|
202
|
+
mask_draw = ImageDraw.Draw(mask)
|
|
203
|
+
mask_draw.ellipse((0, 0, logoSize, logoSize), fill=255)
|
|
204
|
+
|
|
205
|
+
# 创建带透明背景的图标
|
|
206
|
+
logoWithAlpha = Image.new('RGBA', (logoSize, logoSize), (255, 255, 255, 0))
|
|
207
|
+
logoWithAlpha.paste(logo, (0, 0), mask)
|
|
208
|
+
|
|
209
|
+
# 计算位置并粘贴
|
|
210
|
+
logoPosition = ((qrWidth - logoSize) // 2, (qrHeight - logoSize) // 2)
|
|
211
|
+
qrImg.paste(logoWithAlpha, logoPosition, logoWithAlpha)
|
|
212
|
+
|
|
213
|
+
if qrDesc:
|
|
214
|
+
return self.addText(qrImg,qrDesc,self.marginTop,self.marginButtom,self.fontSize,self.fontColor,self.backColor)
|
|
215
|
+
else:
|
|
216
|
+
return qrImg
|
|
217
|
+
|