初版:上传接口(从线上实例导出,适配容器端口)
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
"""
|
||||
图标自动生成
|
||||
"""
|
||||
import os
|
||||
from config import BASE_DIR, IMG_DIR, PIL_AVAILABLE
|
||||
|
||||
|
||||
def generate_favicons():
|
||||
"""根据 favicon.png 自动生成各种尺寸的图标文件"""
|
||||
if not PIL_AVAILABLE:
|
||||
print("⚠️ PIL未安装,跳过图标生成")
|
||||
return
|
||||
|
||||
from PIL import Image, ImageDraw
|
||||
|
||||
source_path = os.path.join(IMG_DIR, 'favicon.png')
|
||||
|
||||
if not os.path.exists(source_path):
|
||||
print("⚠️ 未找到 favicon.png,跳过图标生成")
|
||||
print(" 如需自定义图标,请放置 favicon.png 到 static/img/ 目录")
|
||||
return
|
||||
|
||||
try:
|
||||
img = Image.open(source_path)
|
||||
if img.mode != 'RGBA':
|
||||
img = img.convert('RGBA')
|
||||
|
||||
icon_specs = [
|
||||
('favicon-16x16.png', 16, 16),
|
||||
('favicon-32x32.png', 32, 32),
|
||||
('apple-touch-icon.png', 180, 180),
|
||||
('android-chrome-192x192.png', 192, 192),
|
||||
('android-chrome-512x512.png', 512, 512),
|
||||
]
|
||||
|
||||
generated = []
|
||||
|
||||
for filename, width, height in icon_specs:
|
||||
output_path = os.path.join(IMG_DIR, filename)
|
||||
if os.path.exists(output_path):
|
||||
continue
|
||||
|
||||
resized = img.resize((width, height), Image.Resampling.LANCZOS)
|
||||
|
||||
if 'apple-touch-icon' in filename:
|
||||
mask = Image.new('L', (width, height), 0)
|
||||
draw = ImageDraw.Draw(mask)
|
||||
radius = width // 5
|
||||
draw.rounded_rectangle([0, 0, width, height], radius=radius, fill=255)
|
||||
|
||||
output = Image.new('RGBA', (width, height), (255, 255, 255, 0))
|
||||
output.paste(resized, (0, 0))
|
||||
output.putalpha(mask)
|
||||
resized = output
|
||||
|
||||
resized.save(output_path, 'PNG', optimize=True)
|
||||
generated.append(filename)
|
||||
|
||||
ico_path = os.path.join(IMG_DIR, 'favicon.ico')
|
||||
if not os.path.exists(ico_path):
|
||||
sizes = [(16, 16), (32, 32), (48, 48), (64, 64)]
|
||||
ico_images = []
|
||||
|
||||
for size in sizes:
|
||||
resized = img.resize(size, Image.Resampling.LANCZOS)
|
||||
if resized.mode == 'RGBA':
|
||||
background = Image.new('RGB', size, (255, 255, 255))
|
||||
background.paste(resized, mask=resized.split()[3])
|
||||
resized = background
|
||||
elif resized.mode != 'RGB':
|
||||
resized = resized.convert('RGB')
|
||||
ico_images.append(resized)
|
||||
|
||||
ico_images[0].save(
|
||||
ico_path,
|
||||
format='ICO',
|
||||
sizes=sizes,
|
||||
append_images=ico_images[1:]
|
||||
)
|
||||
generated.append('favicon.ico')
|
||||
|
||||
if generated:
|
||||
print(f"✅ 已生成图标文件: {', '.join(generated)}")
|
||||
else:
|
||||
print("✅ 所有图标文件已存在,跳过生成")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 图标生成失败: {str(e)}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
Reference in New Issue
Block a user