Created
October 16, 2024 23:45
-
-
Save daniel-endraws/05387b507420cb09b9b73cf16e929768 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from aiohttp import web | |
| import aiohttp_cors | |
| from tqdm import tqdm | |
| from PIL import Image | |
| import os | |
| # palette avoids glitchy dithering in gif | |
| # ffmpeg -i frame-%d.png -vf "split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" output.gif | |
| N_FRAMES = 32 | |
| OUT_DIR = "frames" | |
| app = web.Application() | |
| async def receive_file(ws: web.WebSocketResponse) -> tuple[str, bytes]: | |
| name = await ws.receive_str() | |
| data = await ws.receive_bytes() | |
| return name, data | |
| async def websocket_handler(request): | |
| print("connected") | |
| ws = web.WebSocketResponse() | |
| await ws.prepare(request) | |
| # Ignore first frame | |
| await receive_file(ws) | |
| for i in tqdm(range(N_FRAMES)): | |
| _, data = await receive_file(ws) | |
| img = Image.frombytes("RGBA", (736, 520), data) | |
| img = img.crop((46, 33, 686, 513)) | |
| img.save(f"{OUT_DIR}/frame-{i}.png") | |
| await ws.close() | |
| return ws | |
| app.add_routes([web.get('/', websocket_handler)]) | |
| cors = aiohttp_cors.setup(app, defaults={ | |
| "*": aiohttp_cors.ResourceOptions( | |
| allow_credentials=True, | |
| expose_headers="*", | |
| allow_headers="*" | |
| ) | |
| }) | |
| for route in list(app.router.routes()): | |
| cors.add(route) | |
| if __name__ == '__main__': | |
| os.makedirs(OUT_DIR, exist_ok=True) | |
| web.run_app(app, host="0.0.0.0", port=5000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment