26 lines
794 B
Python
26 lines
794 B
Python
from __future__ import annotations
|
|
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
from PIL import Image
|
|
|
|
|
|
def save_reference_image(image_tensor) -> str:
|
|
if image_tensor is None:
|
|
raise ValueError("reference_image is not connected")
|
|
|
|
try:
|
|
import numpy as np
|
|
except ImportError as exc:
|
|
raise RuntimeError("numpy is required by ComfyUI for reference image conversion") from exc
|
|
|
|
first = image_tensor[0] if hasattr(image_tensor, "__getitem__") else image_tensor
|
|
if hasattr(first, "detach"):
|
|
first = first.detach().cpu().numpy()
|
|
|
|
array = np.clip(first * 255.0, 0, 255).astype(np.uint8)
|
|
path = Path(tempfile.NamedTemporaryFile(prefix="comfy_tg_ref_", suffix=".png", delete=False).name)
|
|
Image.fromarray(array).save(path)
|
|
return str(path)
|