forked from arti/vt1200
26 lines
854 B
Python
26 lines
854 B
Python
"""Write human-viewable previews of the logical frames."""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from typing import Sequence
|
|
|
|
import numpy as np
|
|
from PIL import Image
|
|
|
|
|
|
def write_previews(out_dir: str, frames: Sequence[np.ndarray],
|
|
indices: Sequence[int], scale: int = 4) -> list[str]:
|
|
os.makedirs(out_dir, exist_ok=True)
|
|
written: list[str] = []
|
|
for index in indices:
|
|
if index < 0 or index >= len(frames):
|
|
continue
|
|
gray = np.where(frames[index], 0, 255).astype(np.uint8)
|
|
image = Image.fromarray(gray, mode="L")
|
|
image = image.resize((gray.shape[1] * scale, gray.shape[0] * scale),
|
|
Image.Resampling.NEAREST)
|
|
path = os.path.join(out_dir, "frame_%04d.png" % index)
|
|
image.save(path)
|
|
written.append(path)
|
|
return written
|