Coverage for src/io_collection/save/save_gif.py: 100%
12 statements
« prev ^ index » next coverage.py v7.5.1, created at 2024-09-25 19:09 +0000
« prev ^ index » next coverage.py v7.5.1, created at 2024-09-25 19:09 +0000
1import io
3from PIL import Image
5from io_collection.load.load_buffer import load_buffer
6from io_collection.save.save_buffer import save_buffer
9def save_gif(location: str, key: str, frame_keys: list[str]) -> None:
10 """
11 Save series of images as gif to key at specified location.
13 Method will save to the S3 bucket if the location begins with the **s3://**
14 protocol, otherwise it assumes the location is a local path.
16 Parameters
17 ----------
18 location
19 Object location (local path or S3 bucket).
20 key
21 Object key ending in `.gif`.
22 frame_keys
23 List of frame keys to include in the gif.
24 """
26 if not key.endswith(".gif"):
27 message = f"key [ {key} ] must have [ gif ] extension"
28 raise ValueError(message)
30 with io.BytesIO() as buffer:
31 frames = [Image.open(load_buffer(location, frame_key)) for frame_key in frame_keys]
32 frames[0].save(
33 buffer, format="gif", save_all=True, append_images=frames[1:], duration=100, loop=0
34 )
35 save_buffer(location, key, buffer, "image/gif")