File size: 1,761 Bytes
dd96d2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Génère des versions basse résolution de MAP.png pour le chargement progressif.

  MAP_quarter.png  →  1/4 de la taille originale  (chargement rapide ~1-2 Mo)
  MAP_half.png     →  1/2 de la taille originale  (qualité intermédiaire ~7 Mo)

Usage :
  cd backend && python -m scripts.generate_map_lod
  cd backend && python -m scripts.generate_map_lod --input static/MAP.png
"""
from __future__ import annotations

import argparse
import sys
from pathlib import Path

from PIL import Image

_backend = Path(__file__).resolve().parent.parent
if str(_backend) not in sys.path:
    sys.path.insert(0, str(_backend))


def generate_lods(input_path: Path) -> None:
    src = Image.open(input_path).convert("RGB")
    w, h = src.size
    size_mb = input_path.stat().st_size / 1024 / 1024
    print(f"Source : {w}×{h}  ({size_mb:.1f} Mo)  →  {input_path}")

    lods = [
        ("MAP_half.png",    (w // 2, h // 2)),
        ("MAP_quarter.png", (w // 4, h // 4)),
    ]

    for filename, size in lods:
        out_path = input_path.parent / filename
        resized = src.resize(size, Image.LANCZOS)
        resized.save(out_path, format="PNG", optimize=True)
        out_mb = out_path.stat().st_size / 1024 / 1024
        print(f"  {filename:25s}  {size[0]}×{size[1]}  ({out_mb:.1f} Mo)  →  {out_path}")


def main() -> None:
    default = _backend / "static" / "MAP.png"
    parser = argparse.ArgumentParser(description="Génère MAP_half.png et MAP_quarter.png")
    parser.add_argument("--input", type=Path, default=default)
    args = parser.parse_args()
    if not args.input.is_file():
        raise SystemExit(f"Fichier introuvable : {args.input}")
    generate_lods(args.input)


if __name__ == "__main__":
    main()