Pi4 / scripts /build_example_covers.py
yyfz233's picture
Add six project-page RGB examples and responsive gallery
6bab08f
Raw History Blame Contribute Delete
1.17 kB
import base64
from io import BytesIO
import json
from pathlib import Path
import cv2
from PIL import Image, ImageOps
ROOT = Path(__file__).resolve().parents[1]
EXAMPLES = json.loads((ROOT / "examples" / "catalog.json").read_text())
def build_covers():
covers = {}
for example in EXAMPLES:
filename = example["file"]
video = cv2.VideoCapture(str(ROOT / "examples" / filename))
try:
video.set(cv2.CAP_PROP_POS_FRAMES, example["cover_frame"])
readable, frame = video.read()
if not readable:
raise RuntimeError(f"Cannot read the cover frame from {filename}")
finally:
video.release()
image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
image = ImageOps.pad(image, (512, 288), method=Image.Resampling.LANCZOS, color="#ede9f1")
encoded = BytesIO()
image.save(encoded, format="JPEG", quality=82)
covers[example["key"]] = "data:image/jpeg;base64," + base64.b64encode(encoded.getvalue()).decode("ascii")
return json.dumps(covers, indent=2) + "\n"
if __name__ == "__main__":
print(build_covers(), end="")