format-hub / scripts /make_example.py
katospiegel's picture
Deploy format-hub as an imaging-plaza Gradio Space (SDSC)
7d317f1 verified
Raw
History Blame Contribute Delete
980 Bytes
"""Synthesize a small RGB example image and bake it into APP_TMP_DIR/example.tif.
Run at Docker build time (`python -m scripts.make_example`). The example doubles
as the converter's input for the UI, smoke test, and validator.
"""
from __future__ import annotations
import numpy as np
import tifffile
from core.io import APP_TMP_DIR, register_protected
def make_rgb(h: int = 128, w: int = 128) -> np.ndarray:
yy, xx = np.mgrid[0:h, 0:w].astype(np.float32)
r = (np.sin(xx / 12.0) * 0.5 + 0.5)
g = (np.cos(yy / 16.0) * 0.5 + 0.5)
b = ((xx + yy) / (h + w))
rgb = np.stack([r, g, b], axis=-1)
return (np.clip(rgb, 0, 1) * 255).astype(np.uint8)
def main() -> int:
out = APP_TMP_DIR / "example.tif"
rgb = make_rgb()
tifffile.imwrite(out, rgb)
register_protected(out) # survive the build process's atexit temp wipe
print(f"[make_example] wrote {out} {rgb.shape}")
return 0
if __name__ == "__main__":
raise SystemExit(main())