Download utils.py from cgeorgiaw/poseidon: direct link, hf CLI and curl.
- Browser
- Download file 729 Bytes
-
https://huggingface.co/spaces/cgeorgiaw/poseidon/resolve/main/utils.py
- Command line
-
hf download hf://spaces/cgeorgiaw/poseidon/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/cgeorgiaw/poseidon/resolve/main/utils.py
729 Bytes
| import io | |
| import os | |
| from PIL import Image | |
| def fig2img(fig): | |
| """Convert a Matplotlib figure to a PIL Image and return it""" | |
| # Save figure to a temporary buffer. | |
| buf = io.BytesIO() | |
| fig.savefig(buf) | |
| buf.seek(0) | |
| # Create PIL image from buffer | |
| img = Image.open(buf) | |
| return img | |
| def create_gif_from_frames(frames, filename): | |
| """Create a GIF from a list of PIL Image frames""" | |
| # Create output directory if it doesn't exist | |
| os.makedirs('episode_gifs', exist_ok=True) | |
| # Save the frames as GIF | |
| frames[0].save( | |
| f'{filename}', | |
| save_all=True, | |
| append_images=frames[1:], | |
| duration=200, # Duration for each frame in milliseconds | |
| loop=0 | |
| ) | |