-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_utils.py
More file actions
97 lines (81 loc) · 3.23 KB
/
Copy pathimage_utils.py
File metadata and controls
97 lines (81 loc) · 3.23 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""aiseed web creator — ユーティリティ&画像最適化"""
import json, platform, shutil
from pathlib import Path
BASE_DIR = Path(__file__).parent
TEMPLATES_DIR = BASE_DIR / "templates"
CONFIG_FILE = BASE_DIR / "config.json"
def default_projects_dir():
"""OS別のデフォルトプロジェクト保存先を返す"""
system = platform.system()
if system == "Windows":
return Path.home() / "Documents" / "aiseed-websites"
elif system == "Darwin": # macOS
return Path.home() / "Documents" / "aiseed-websites"
else: # Linux
return Path.home() / "aiseed-websites"
def _resolve_projects_dir():
cfg = {}
if CONFIG_FILE.exists():
try: cfg = json.loads(CONFIG_FILE.read_text("utf-8"))
except Exception: pass
custom = cfg.get("projects_dir", "")
if custom:
p = Path(custom).expanduser().resolve()
p.mkdir(parents=True, exist_ok=True)
return p
return default_projects_dir()
PROJECTS_DIR = _resolve_projects_dir()
def load_json(path, default=None):
if path.exists(): return json.loads(path.read_text("utf-8"))
return default if default is not None else {}
def save_json(path, data):
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(data, ensure_ascii=False, indent=2), "utf-8")
def list_templates():
tpls = []
if TEMPLATES_DIR.exists():
for f in sorted(TEMPLATES_DIR.glob("*.json")):
try:
t = json.loads(f.read_text("utf-8"))
tpls.append({"path": f, "id": t.get("template_id",""), "name": t.get("template_name",""),
"icon": t.get("icon","📄")})
except: pass
return tpls
def optimize_image(src_path: Path, dest_dir: Path, max_width=1600, quality=82):
"""画像をWebP変換+リサイズ。Pillow未インストール時はそのままコピー。
Returns: 保存先の相対パス (images/xxx.webp or images/xxx.jpg)"""
dest_dir.mkdir(parents=True, exist_ok=True)
try:
from PIL import Image
img = Image.open(src_path)
if src_path.suffix.lower() == ".svg":
dest = dest_dir / src_path.name
shutil.copy2(src_path, dest)
return f"images/{src_path.name}"
if img.mode in ("RGBA", "P"):
img = img.convert("RGBA")
fmt, ext = "WEBP", ".webp"
else:
img = img.convert("RGB")
fmt, ext = "WEBP", ".webp"
if img.width > max_width:
ratio = max_width / img.width
img = img.resize((max_width, int(img.height * ratio)), Image.LANCZOS)
stem = src_path.stem
dest = dest_dir / f"{stem}{ext}"
n = 1
while dest.exists():
dest = dest_dir / f"{stem}_{n}{ext}"
n += 1
img.save(dest, fmt, quality=quality)
# サムネイル生成
thumb = img.copy()
thumb.thumbnail((400, 400), Image.LANCZOS)
thumb_dir = dest_dir / "thumbs"
thumb_dir.mkdir(exist_ok=True)
thumb.save(thumb_dir / dest.name, fmt, quality=70)
return f"images/{dest.name}"
except ImportError:
dest = dest_dir / src_path.name
shutil.copy2(src_path, dest)
return f"images/{src_path.name}"