Files
sprinkler-calculation/tools/make_icons.py
T
2026-11-16 01:39:59 +07:00

56 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""สร้างไอคอน PWA จากโลโก้เดิมในโปรเจกต์
ต้นทาง: logo/logo ใหม่/2566-09-08 11-51-40.png (1500×1500, RGBA)
ปลายทาง: assets/icons/
icon-192.png 192×192 (any)
icon-512.png 512×512 (any)
icon-maskable-512.png 512×512 (maskable — โลโก้อยู่ใน 78% กลางเฟรม)
apple-touch-icon-180.png 180×180
ใช้: python tools/make_icons.py
"""
import os
from PIL import Image
SOURCE = os.path.join("logo", "logo ใหม่", "2566-09-08 11-51-40.png")
OUT_DIR = os.path.join("assets", "icons")
BACKGROUND = (255, 255, 255, 255) # ขาวทึบ (ไอคอนโปร่งใสมักแสดงผลเพี้ยนบน launcher)
MASKABLE_SAFE_RATIO = 0.78
def flatten(source, size, ratio=1.0):
"""ย่อ/ขยายโลโก้ให้อยู่กลางเฟรมจตุรัสบนพื้นทึบ"""
canvas = Image.new("RGBA", (size, size), BACKGROUND)
inner = int(size * ratio)
logo = source.copy()
logo.thumbnail((inner, inner), Image.LANCZOS)
offset = ((size - logo.width) // 2, (size - logo.height) // 2)
canvas.paste(logo, offset, logo)
return canvas.convert("RGB")
def main():
os.makedirs(OUT_DIR, exist_ok=True)
source = Image.open(SOURCE).convert("RGBA")
targets = [
("icon-192.png", 192, 1.0),
("icon-512.png", 512, 1.0),
("icon-maskable-512.png", 512, MASKABLE_SAFE_RATIO),
("apple-touch-icon-180.png", 180, 1.0),
]
for name, size, ratio in targets:
image = flatten(source, size, ratio)
path = os.path.join(OUT_DIR, name)
image.save(path, "PNG", optimize=True)
print("%-26s %sx%s %6.1f KB" % (name, image.width, image.height,
os.path.getsize(path) / 1024))
if __name__ == "__main__":
main()