Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from rembg import remove
|
3 |
+
from PIL import Image
|
4 |
+
from io import BytesIO
|
5 |
+
import base64
|
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. This code is open source and available [here](https://github.com/tyler-simons/BackgroundRemoval) on GitHub. Special thanks to the [rembg library](https://github.com/danielgatis/rembg) :grin:"
|
12 |
+
)
|
13 |
+
st.sidebar.write("## Upload and download :gear:")
|
14 |
+
|
15 |
+
MAX_FILE_SIZE = 5 * 1024 * 1024 # 5MB
|
16 |
+
|
17 |
+
# Download the fixed image
|
18 |
+
def convert_image(img):
|
19 |
+
buf = BytesIO()
|
20 |
+
img.save(buf, format="PNG")
|
21 |
+
byte_im = buf.getvalue()
|
22 |
+
return byte_im
|
23 |
+
|
24 |
+
|
25 |
+
def fix_image(upload):
|
26 |
+
image = Image.open(upload)
|
27 |
+
col1.write("Original Image :camera:")
|
28 |
+
col1.image(image)
|
29 |
+
|
30 |
+
fixed = remove(image)
|
31 |
+
col2.write("Fixed Image :wrench:")
|
32 |
+
col2.image(fixed)
|
33 |
+
st.sidebar.markdown("\n")
|
34 |
+
st.sidebar.download_button("Download fixed image", convert_image(fixed), "fixed.png", "image/png")
|
35 |
+
|
36 |
+
|
37 |
+
col1, col2 = st.columns(2)
|
38 |
+
my_upload = st.sidebar.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
39 |
+
|
40 |
+
if my_upload is not None:
|
41 |
+
if my_upload.size > MAX_FILE_SIZE:
|
42 |
+
st.error("The uploaded file is too large. Please upload an image smaller than 5MB.")
|
43 |
+
else:
|
44 |
+
fix_image(upload=my_upload)
|
45 |
+
else:
|
46 |
+
fix_image("./plant.png")
|