2022-02-27 17:34:19 +08:00
|
|
|
import platform
|
|
|
|
|
import pypinyin
|
2022-12-24 00:16:17 +08:00
|
|
|
from pathlib import Path
|
2022-02-27 17:34:19 +08:00
|
|
|
from PIL.ImageFont import FreeTypeFont
|
|
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
|
from PIL.Image import Image as IMG
|
2022-12-24 00:16:17 +08:00
|
|
|
|
2022-02-27 17:34:19 +08:00
|
|
|
from configs.path_config import FONT_PATH
|
|
|
|
|
|
2022-12-24 00:16:17 +08:00
|
|
|
dir_path = Path(__file__).parent.absolute()
|
|
|
|
|
|
2022-02-27 17:34:19 +08:00
|
|
|
|
|
|
|
|
def cn2py(word) -> str:
|
2022-12-24 00:16:17 +08:00
|
|
|
"""保存声调,防止出现类似方舟干员红与吽拼音相同声调不同导致红照片无法保存的问题"""
|
2022-02-27 17:34:19 +08:00
|
|
|
temp = ""
|
2022-12-24 00:16:17 +08:00
|
|
|
for i in pypinyin.pinyin(word, style=pypinyin.Style.TONE3):
|
2022-02-27 17:34:19 +08:00
|
|
|
temp += "".join(i)
|
|
|
|
|
return temp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 移除windows和linux下特殊字符
|
|
|
|
|
def remove_prohibited_str(name: str) -> str:
|
|
|
|
|
if platform.system().lower() == "windows":
|
|
|
|
|
tmp = ""
|
|
|
|
|
for i in name:
|
|
|
|
|
if i not in ["\\", "/", ":", "*", "?", '"', "<", ">", "|"]:
|
|
|
|
|
tmp += i
|
|
|
|
|
name = tmp
|
|
|
|
|
else:
|
|
|
|
|
name = name.replace("/", "\\")
|
|
|
|
|
return name
|
|
|
|
|
|
|
|
|
|
|
2022-12-24 00:16:17 +08:00
|
|
|
def load_font(fontname: str = "msyh.ttf", fontsize: int = 16) -> FreeTypeFont:
|
2022-02-27 17:34:19 +08:00
|
|
|
return ImageFont.truetype(
|
2022-12-24 00:16:17 +08:00
|
|
|
str(FONT_PATH / f"{fontname}"), fontsize, encoding="utf-8"
|
2022-02-27 17:34:19 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def circled_number(num: int) -> IMG:
|
|
|
|
|
font = load_font(fontsize=450)
|
|
|
|
|
text = str(num)
|
|
|
|
|
text_w = font.getsize(text)[0]
|
|
|
|
|
w = 240 + text_w
|
|
|
|
|
w = w if w >= 500 else 500
|
|
|
|
|
img = Image.new("RGBA", (w, 500))
|
|
|
|
|
draw = ImageDraw.Draw(img)
|
|
|
|
|
draw.ellipse(((0, 0), (500, 500)), fill="red")
|
|
|
|
|
draw.ellipse(((w - 500, 0), (w, 500)), fill="red")
|
|
|
|
|
draw.rectangle(((250, 0), (w - 250, 500)), fill="red")
|
|
|
|
|
draw.text(
|
|
|
|
|
(120, -60),
|
|
|
|
|
text,
|
|
|
|
|
font=font,
|
|
|
|
|
fill="white",
|
|
|
|
|
stroke_width=10,
|
|
|
|
|
stroke_fill="white",
|
|
|
|
|
)
|
|
|
|
|
return img
|
|
|
|
|
|