#!/usr/bin/env python3 """Convert a source image into a DXT1 (BC1) DDS loading screen. Center-crops the source to the target aspect ratio, resizes to WxH with Lanczos, then compresses to DXT1 via Microsoft's texconv (DirectXTex, MIT). DXT1 is 8:1 compression (half of DXT5's 4:1) with identical color quality for opaque images; loading screens need no alpha. The DDS stores sRGB-encoded bytes directly (no linear round-trip), matching how vanilla EU4 loading screens are encoded. Usage: python convert_loadingscreen_dds.py Requires tools/bin/texconv.exe next to this script. """ import os import subprocess import sys import tempfile from PIL import Image HERE = os.path.dirname(os.path.abspath(__file__)) TEXCONV = os.path.join(HERE, "bin", "texconv.exe") def center_crop_to_aspect(im: Image.Image, w: int, h: int) -> Image.Image: sw, sh = im.size target_ar = w / h src_ar = sw / sh if src_ar > target_ar: # Source is wider than target: trim sides, keep full height. new_w = round(sh * target_ar) x0 = (sw - new_w) // 2 box = (x0, 0, x0 + new_w, sh) else: # Source is taller than target: trim top/bottom, keep full width. new_h = round(sw / target_ar) y0 = (sh - new_h) // 2 box = (0, y0, sw, y0 + new_h) return im.crop(box) def main() -> int: if len(sys.argv) != 5: print(__doc__) return 2 src, dst, w_s, h_s = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4] w, h = int(w_s), int(h_s) if not os.path.isfile(TEXCONV): print(f"error: texconv not found at {TEXCONV}", file=sys.stderr) return 1 im = Image.open(src) if im.mode not in ("RGB", "RGBA"): im = im.convert("RGB") print(f"source: {im.size[0]}x{im.size[1]} {im.mode} ({os.path.basename(src)})") im = center_crop_to_aspect(im, w, h) im = im.resize((w, h), Image.LANCZOS) if im.mode == "RGBA": # Loading screens are opaque; drop alpha so DXT1 has no transparency artifacts. im = im.convert("RGB") print(f"resized: {im.size[0]}x{im.size[1]}") with tempfile.TemporaryDirectory() as tmp: stage = os.path.join(tmp, "stage.png") im.save(stage, format="PNG") # -f DXT1 : BC1/DXT1 - 8:1 compression, opaque, half the size of DXT5 # -m 1 : top level only, no mip chain (loading screens never minify) # -ft dds : DDS output # -o tmp : write into the scratch dir, then rename to the final name # (keeps texconv on ASCII paths; the final unicode rename is # handled by Python). cmd = [TEXCONV, "-f", "DXT1", "-m", "1", "-ft", "dds", "-y", "-o", tmp, stage] print("texconv:", " ".join(cmd)) r = subprocess.run(cmd, capture_output=True, text=True) if r.returncode != 0: sys.stdout.write(r.stdout) sys.stderr.write(r.stderr) return r.returncode produced = os.path.join(tmp, "stage.dds") if not os.path.isfile(produced): print("error: texconv produced no output", file=sys.stderr) sys.stderr.write(r.stderr) return 1 os.replace(produced, dst) print(f"wrote: {dst} ({os.path.getsize(dst)} bytes)") return 0 if __name__ == "__main__": raise SystemExit(main())