Files
Todo-Project/generate_icons.py
Hyungi Ahn 5c9ea92fb8 백업용
2025-09-19 09:13:44 +09:00

116 lines
4.0 KiB
Python

#!/usr/bin/env python3
"""
PWA 아이콘 생성 스크립트
원본 이미지를 다양한 크기의 아이콘으로 변환
"""
from PIL import Image, ImageDraw, ImageFilter
import os
def create_rounded_icon(image, size, corner_radius=None):
"""둥근 모서리 아이콘 생성"""
if corner_radius is None:
corner_radius = size // 8 # 기본값: 크기의 1/8
# 이미지 리사이즈
img = image.resize((size, size), Image.Resampling.LANCZOS)
# 마스크 생성 (둥근 사각형)
mask = Image.new('L', (size, size), 0)
draw = ImageDraw.Draw(mask)
draw.rounded_rectangle([0, 0, size, size], corner_radius, fill=255)
# 마스크 적용
result = Image.new('RGBA', (size, size), (0, 0, 0, 0))
result.paste(img, (0, 0))
result.putalpha(mask)
return result
def main():
# 원본 이미지 로드
source_image = "DSCF0333.RAF_compressed.JPEG"
if not os.path.exists(source_image):
print(f"❌ 원본 이미지를 찾을 수 없습니다: {source_image}")
return
try:
with Image.open(source_image) as img:
# RGB로 변환 (RGBA가 아닌 경우)
if img.mode != 'RGB':
img = img.convert('RGB')
# 정사각형으로 크롭 (중앙 기준)
width, height = img.size
size = min(width, height)
left = (width - size) // 2
top = (height - size) // 2
img = img.crop((left, top, left + size, top + size))
# 아이콘 크기 목록
icon_sizes = [
(72, "icon-72x72.png"),
(96, "icon-96x96.png"),
(128, "icon-128x128.png"),
(144, "icon-144x144.png"),
(152, "icon-152x152.png"),
(192, "icon-192x192.png"),
(384, "icon-384x384.png"),
(512, "icon-512x512.png")
]
# Apple Touch Icon (둥근 모서리 없음)
apple_sizes = [
(180, "apple-touch-icon.png"),
(167, "apple-touch-icon-ipad.png")
]
# 아이콘 디렉토리 생성
icons_dir = "frontend/static/icons"
os.makedirs(icons_dir, exist_ok=True)
print("🎨 PWA 아이콘 생성 중...")
# PWA 아이콘 생성 (둥근 모서리)
for size, filename in icon_sizes:
icon = create_rounded_icon(img, size)
icon_path = os.path.join(icons_dir, filename)
icon.save(icon_path, "PNG", optimize=True)
print(f"{filename} ({size}x{size})")
# Apple Touch Icon 생성 (둥근 모서리 없음)
for size, filename in apple_sizes:
icon = img.resize((size, size), Image.Resampling.LANCZOS)
icon_path = os.path.join(icons_dir, filename)
icon.save(icon_path, "PNG", optimize=True)
print(f"{filename} ({size}x{size})")
# 파비콘 생성
favicon_sizes = [(16, 16), (32, 32), (48, 48)]
favicon_images = []
for size, _ in favicon_sizes:
favicon = img.resize((size, size), Image.Resampling.LANCZOS)
favicon_images.append(favicon)
# 멀티 사이즈 favicon.ico 생성
favicon_path = "frontend/favicon.ico"
favicon_images[0].save(
favicon_path,
format='ICO',
sizes=[(16, 16), (32, 32), (48, 48)],
append_images=favicon_images[1:]
)
print(f"✅ favicon.ico (16x16, 32x32, 48x48)")
print(f"\n🎉 총 {len(icon_sizes) + len(apple_sizes) + 1}개의 아이콘이 생성되었습니다!")
print(f"📁 아이콘 위치: {icons_dir}/")
except Exception as e:
print(f"❌ 아이콘 생성 실패: {e}")
if __name__ == "__main__":
main()