-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
55 lines (47 loc) · 1.13 KB
/
Copy pathutils.py
File metadata and controls
55 lines (47 loc) · 1.13 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import base64
from io import BytesIO
import streamlit as st
from PIL import Image
def st_dl_png(im: Image.Image, stem: str, key=str, label: str = "Download") -> None:
buf = BytesIO()
im.save(buf, format='png')
byte_im = buf.getvalue()
st.download_button(
label=label,
data=byte_im,
file_name=f"{stem}.png",
mime='image/png',
key=key,
)
def st_dl_gif(
ims: list[Image.Image],
duration: float,
stem: str,
key=str,
label: str = "Download",
show_gif: bool = True,
) -> None:
buf = BytesIO()
ims[0].save(
buf,
format='gif',
save_all=True,
append_images=ims[1:],
optimize=False,
duration=duration,
loop=0,
)
byte_im = buf.getvalue()
if show_gif:
data_url = base64.b64encode(byte_im).decode('utf-8')
st.markdown(
f"<img src='data:image/gif;base64,{data_url}' alt='gif'>",
unsafe_allow_html=True,
)
st.download_button(
label=label,
data=byte_im,
file_name=f"{stem}.gif",
mime='image/gif',
key=key,
)