Spaces:
Sleeping
Sleeping
noumanjavaid
commited on
Commit
•
3fdb4ef
1
Parent(s):
21e95d8
Upload streamlit_app.py
Browse files- streamlit_app.py +49 -0
streamlit_app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from io import BytesIO
|
2 |
+
|
3 |
+
import streamlit as st
|
4 |
+
from PIL import Image
|
5 |
+
from rembg import remove
|
6 |
+
|
7 |
+
st.set_page_config(layout="wide", page_title="Image Background Remover")
|
8 |
+
|
9 |
+
st.write("## Remove background from your image")
|
10 |
+
st.write(
|
11 |
+
":dog: Try uploading an image to watch the background magically removed. Full quality images can be downloaded from the sidebar. "
|
12 |
+
)
|
13 |
+
|
14 |
+
st.sidebar.write("## Upload and download :gear:")
|
15 |
+
|
16 |
+
# Create the columns
|
17 |
+
col1, col2 = st.columns(2)
|
18 |
+
|
19 |
+
# Download the fixed image
|
20 |
+
def convert_image(img):
|
21 |
+
buf = BytesIO()
|
22 |
+
img.save(buf, format="PNG")
|
23 |
+
byte_im = buf.getvalue()
|
24 |
+
return byte_im
|
25 |
+
|
26 |
+
# Package the transform into a function
|
27 |
+
def fix_image(upload):
|
28 |
+
image = Image.open(upload)
|
29 |
+
col1.write("Original Image :camera:")
|
30 |
+
col1.image(image)
|
31 |
+
|
32 |
+
fixed = remove(image)
|
33 |
+
col2.write("Fixed Image :wrench:")
|
34 |
+
col2.image(fixed)
|
35 |
+
st.sidebar.markdown("\\n")
|
36 |
+
st.sidebar.download_button(
|
37 |
+
"Download fixed image",
|
38 |
+
convert_image(fixed),
|
39 |
+
"fixed.png",
|
40 |
+
"image/png",
|
41 |
+
)
|
42 |
+
|
43 |
+
# Upload the file
|
44 |
+
image_upload = st.sidebar.file_uploader(
|
45 |
+
"Upload an image", type=["png", "jpg", "jpeg"]
|
46 |
+
)
|
47 |
+
|
48 |
+
if image_upload:
|
49 |
+
fix_image(image_upload)
|