Download scripts/build_example_covers.py from yyfz233/Pi4: direct link, hf CLI and curl.
- Browser
- Download file 1.17 kB
-
https://huggingface.co/spaces/yyfz233/Pi4/resolve/main/scripts/build_example_covers.py
- Command line
-
hf download hf://spaces/yyfz233/Pi4/scripts/build_example_covers.py
-
curl -L -o build_example_covers.py https://huggingface.co/spaces/yyfz233/Pi4/resolve/main/scripts/build_example_covers.py
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="") | |