File size: 1,166 Bytes
1d5871f 6bab08f 1d5871f 6bab08f 1d5871f 6bab08f 1d5871f 6bab08f 1d5871f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 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="")
|