Files
AiVideo/engine/adapters/image/mock_adapter.py
2026-03-25 19:35:37 +08:00

46 lines
1.3 KiB
Python

from __future__ import annotations
import os
import uuid
from pathlib import Path
from urllib.request import urlopen
from PIL import Image
from .base import BaseImageGen
ASSETS_DIR = "assets"
DEMO_IMAGE = os.path.join(ASSETS_DIR, "demo.jpg")
def ensure_demo_image() -> None:
os.makedirs(ASSETS_DIR, exist_ok=True)
if os.path.exists(DEMO_IMAGE):
return
url = "https://picsum.photos/1280/720"
with urlopen(url, timeout=30) as resp:
data = resp.read()
with open(DEMO_IMAGE, "wb") as f:
f.write(data)
class MockImageGen(BaseImageGen):
def generate(self, prompt: dict[str, str], output_dir: str | Path) -> str:
# prompt is accepted for interface consistency; mock uses only demo.jpg.
_ = prompt
ensure_demo_image()
out_dir = Path(output_dir)
out_dir.mkdir(parents=True, exist_ok=True)
out_path = out_dir / f"shot_{uuid.uuid4().hex}.png"
try:
# Convert to PNG so verification criteria can match *.png.
img = Image.open(DEMO_IMAGE).convert("RGB")
img.save(str(out_path), format="PNG")
except Exception:
# Last-resort: if PNG conversion fails, still write a best-effort copy.
out_path.write_bytes(Path(DEMO_IMAGE).read_bytes())
return str(out_path)