Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -12,35 +12,26 @@ apply_prod_style(st) # NOTE: Uncomment this for production!
|
|
12 |
def V_SPACE(lines):
|
13 |
for _ in range(lines):
|
14 |
st.write(' ')
|
15 |
-
|
16 |
def image_download_button(pil_image, filename: str, fmt: str, label="Download"):
|
17 |
if fmt not in ["jpg", "png"]:
|
18 |
raise Exception(f"Unknown image format (Available: {fmt} - case sensitive)")
|
19 |
-
|
20 |
-
pil_format = "JPEG" if fmt == "jpg" else "PNG"
|
21 |
-
file_format = "jpg" if fmt == "jpg" else "png"
|
22 |
-
mime = "image/jpeg/?target=external" if fmt == "jpg" else "image/png/?target=external"
|
23 |
-
|
24 |
-
buf = BytesIO()
|
25 |
-
pil_image.save(buf, format=pil_format)
|
26 |
-
|
27 |
-
# Return the binary data as a string
|
28 |
-
return buf.getvalue()
|
29 |
-
|
30 |
-
def download_button(pil_image, filename: str, fmt: str, label="Download"):
|
31 |
-
if fmt not in ["jpg", "png"]:
|
32 |
-
raise Exception(f"Unknown image format (Available: {fmt} - case sensitive)")
|
33 |
-
|
34 |
pil_format = "JPEG" if fmt == "jpg" else "PNG"
|
35 |
file_format = "jpg" if fmt == "jpg" else "png"
|
36 |
mime = "image/jpeg/?target=external" if fmt == "jpg" else "image/png/?target=external"
|
37 |
-
|
38 |
buf = BytesIO()
|
|
|
39 |
pil_image.save(buf, format=pil_format)
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
44 |
|
45 |
uploaded_file = st.file_uploader(
|
46 |
label="Upload your photo here",
|
@@ -53,8 +44,9 @@ if uploaded_file is not None:
|
|
53 |
in_submit = st.button("Submit")
|
54 |
|
55 |
if uploaded_file is not None and in_submit:
|
56 |
-
|
57 |
-
|
|
|
58 |
with st.spinner("AI is doing magic to your photo. Please wait..."):
|
59 |
hexmap = {
|
60 |
"Transparent (PNG)": "#000000",
|
@@ -67,11 +59,11 @@ if uploaded_file is not None:
|
|
67 |
alpha = 0.0 if in_mode == "Transparent (PNG)" else 1.0
|
68 |
img_matte = matte(img_input)
|
69 |
img_output = change_background(img_input, img_matte, background_alpha=alpha, background_hex=hexmap[in_mode])
|
70 |
-
|
71 |
with st.expander("Success!", expanded=True):
|
72 |
st.image(img_output)
|
73 |
uploaded_name = os.path.splitext(uploaded_file.name)[0]
|
74 |
-
|
75 |
pil_image=img_output,
|
76 |
filename=uploaded_name,
|
77 |
fmt="png"
|
|
|
12 |
def V_SPACE(lines):
|
13 |
for _ in range(lines):
|
14 |
st.write(' ')
|
15 |
+
|
16 |
def image_download_button(pil_image, filename: str, fmt: str, label="Download"):
|
17 |
if fmt not in ["jpg", "png"]:
|
18 |
raise Exception(f"Unknown image format (Available: {fmt} - case sensitive)")
|
19 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
pil_format = "JPEG" if fmt == "jpg" else "PNG"
|
21 |
file_format = "jpg" if fmt == "jpg" else "png"
|
22 |
mime = "image/jpeg/?target=external" if fmt == "jpg" else "image/png/?target=external"
|
23 |
+
|
24 |
buf = BytesIO()
|
25 |
+
|
26 |
pil_image.save(buf, format=pil_format)
|
27 |
+
|
28 |
+
return st.download_button(
|
29 |
+
label=label,
|
30 |
+
data=buf.getvalue(),
|
31 |
+
file_name=f'{filename}.{file_format}',
|
32 |
+
mime=mime,
|
33 |
+
)
|
34 |
+
|
35 |
|
36 |
uploaded_file = st.file_uploader(
|
37 |
label="Upload your photo here",
|
|
|
44 |
in_submit = st.button("Submit")
|
45 |
|
46 |
if uploaded_file is not None and in_submit:
|
47 |
+
# Use BytesIO to avoid decoding error
|
48 |
+
img_input = Image.open(BytesIO(uploaded_file.read()))
|
49 |
+
|
50 |
with st.spinner("AI is doing magic to your photo. Please wait..."):
|
51 |
hexmap = {
|
52 |
"Transparent (PNG)": "#000000",
|
|
|
59 |
alpha = 0.0 if in_mode == "Transparent (PNG)" else 1.0
|
60 |
img_matte = matte(img_input)
|
61 |
img_output = change_background(img_input, img_matte, background_alpha=alpha, background_hex=hexmap[in_mode])
|
62 |
+
|
63 |
with st.expander("Success!", expanded=True):
|
64 |
st.image(img_output)
|
65 |
uploaded_name = os.path.splitext(uploaded_file.name)[0]
|
66 |
+
image_download_button(
|
67 |
pil_image=img_output,
|
68 |
filename=uploaded_name,
|
69 |
fmt="png"
|