rishi9440 commited on
Commit
9d62619
1 Parent(s): 8b6650d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -8
app.py CHANGED
@@ -12,25 +12,34 @@ 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
  pil_format = "JPEG" if fmt == "jpg" else "PNG"
20
  file_format = "jpg" if fmt == "jpg" else "png"
21
  mime = "image/jpeg/?target=external" if fmt == "jpg" else "image/png/?target=external"
22
-
23
  buf = BytesIO()
24
 
25
  pil_image.save(buf, format=pil_format)
26
-
27
  return st.download_button(
28
  label=label,
29
  data=buf.getvalue(),
30
  file_name=f'{filename}.{file_format}',
31
  mime=mime,
 
 
32
  )
33
 
 
 
 
 
 
 
34
 
35
  uploaded_file = st.file_uploader(
36
  label="Upload your photo here",
@@ -43,9 +52,8 @@ if uploaded_file is not None:
43
  in_submit = st.button("Submit")
44
 
45
  if uploaded_file is not None and in_submit:
46
- # Use BytesIO to avoid decoding error
47
- img_input = Image.open(BytesIO(uploaded_file.read()))
48
-
49
  with st.spinner("AI is doing magic to your photo. Please wait..."):
50
  hexmap = {
51
  "Transparent (PNG)": "#000000",
@@ -58,9 +66,9 @@ if uploaded_file is not None:
58
  alpha = 0.0 if in_mode == "Transparent (PNG)" else 1.0
59
  img_matte = matte(img_input)
60
  img_output = change_background(img_input, img_matte, background_alpha=alpha, background_hex=hexmap[in_mode])
61
-
62
  with st.expander("Success!", expanded=True):
63
- st.image(img_output+"/?target=external")
64
  uploaded_name = os.path.splitext(uploaded_file.name)[0]
65
  image_download_button(
66
  pil_image=img_output,
 
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
+ on_click=open_in_new_tab,
34
+ args=(buf.getvalue(),)
35
  )
36
 
37
+ def open_in_new_tab(file_content):
38
+ file_ = BytesIO(file_content)
39
+ file_.seek(0)
40
+ b64_img = base64.b64encode(file_.read()).decode()
41
+ href = f'<a href="data:image/png;base64,{b64_img}" target="_blank" rel="noopener noreferrer">Open image in new tab</a>'
42
+ st.sidebar.markdown(href, unsafe_allow_html=True)
43
 
44
  uploaded_file = st.file_uploader(
45
  label="Upload your photo here",
 
52
  in_submit = st.button("Submit")
53
 
54
  if uploaded_file is not None and in_submit:
55
+ img_input = Image.open(uploaded_file)
56
+
 
57
  with st.spinner("AI is doing magic to your photo. Please wait..."):
58
  hexmap = {
59
  "Transparent (PNG)": "#000000",
 
66
  alpha = 0.0 if in_mode == "Transparent (PNG)" else 1.0
67
  img_matte = matte(img_input)
68
  img_output = change_background(img_input, img_matte, background_alpha=alpha, background_hex=hexmap[in_mode])
69
+
70
  with st.expander("Success!", expanded=True):
71
+ st.image(img_output)
72
  uploaded_name = os.path.splitext(uploaded_file.name)[0]
73
  image_download_button(
74
  pil_image=img_output,