Spaces:
Runtime error
Runtime error
File size: 2,276 Bytes
787d365 |
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# 字体管理
# 创建人:曾逸夫
# 创建时间:2022-05-01
import os
import sys
from pathlib import Path
import wget
from rich.console import Console
ROOT_PATH = sys.path[0] # 项目根目录
# 中文、英文、俄语、西班牙语、阿拉伯语、韩语
fonts_list = ["SimSun.ttf", "TimesNewRoman.ttf", "malgun.ttf"] # 字体列表
fonts_suffix = ["ttc", "ttf", "otf"] # 字体后缀
data_url_dict = {
"SimSun.ttf": "https://gitee.com/CV_Lab/gradio_yolov5_det/attach_files/1053539/download/SimSun.ttf",
"TimesNewRoman.ttf": "https://gitee.com/CV_Lab/gradio_yolov5_det/attach_files/1053537/download/TimesNewRoman.ttf",
"malgun.ttf": "https://gitee.com/CV_Lab/gradio_yolov5_det/attach_files/1053538/download/malgun.ttf",}
console = Console()
# 创建字体库
def add_fronts(font_diff):
global font_name
for k, v in data_url_dict.items():
if k in font_diff:
font_name = v.split("/")[-1] # 字体名称
Path(f"{ROOT_PATH}/fonts").mkdir(parents=True, exist_ok=True) # 创建目录
file_path = f"{ROOT_PATH}/fonts/{font_name}" # 字体路径
try:
# 下载字体文件
wget.download(v, file_path)
except Exception as e:
print("路径错误!程序结束!")
print(e)
sys.exit()
else:
print()
console.print(f"{font_name} [bold green]字体文件下载完成![/bold green] 已保存至:{file_path}")
# 判断字体文件
def is_fonts(fonts_dir):
if os.path.isdir(fonts_dir):
# 如果字体库存在
f_list = os.listdir(fonts_dir) # 本地字体库
font_diff = list(set(fonts_list).difference(set(f_list)))
if font_diff != []:
# 字体不存在
console.print("[bold red]字体不存在,正在加载。。。[/bold red]")
add_fronts(font_diff) # 创建字体库
else:
console.print(f"{fonts_list}[bold green]字体已存在![/bold green]")
else:
# 字体库不存在,创建字体库
console.print("[bold red]字体库不存在,正在创建。。。[/bold red]")
add_fronts(fonts_list) # 创建字体库
|