Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Importing required libraries
|
2 |
+
import streamlit as st # For building the web application
|
3 |
+
from rembg import remove # For removing the background from images
|
4 |
+
from PIL import Image # For image handling and processing
|
5 |
+
from io import BytesIO # For handling byte data in memory
|
6 |
+
import base64 # For encoding and decoding binary data
|
7 |
+
|
8 |
+
# Setting the page configuration
|
9 |
+
st.set_page_config(layout="wide", page_title="Image Background Remover") # Sets a wide layout and custom title for the app
|
10 |
+
|
11 |
+
# Title and description of the web app
|
12 |
+
st.write("## Say goodbye to backgrounds!") # Display the main title of the app
|
13 |
+
st.write(
|
14 |
+
"🐾 Upload an image to see the background disappear. Once you're happy, grab the final version from the sidebar."
|
15 |
+
)
|
16 |
+
|
17 |
+
# Sidebar header
|
18 |
+
st.sidebar.write("## Upload and download :gear:") # Sidebar section for uploading and downloading images
|
19 |
+
|
20 |
+
# Setting the maximum file size for uploads
|
21 |
+
MAX_FILE_SIZE = 5 * 1024 * 1024 # Limit the uploaded file size to 5MB
|
22 |
+
|
23 |
+
# Function to convert an image into byte format for downloading
|
24 |
+
def convert_image(img):
|
25 |
+
buf = BytesIO() # Creates an in-memory bytes buffer
|
26 |
+
img.save(buf, format="PNG") # Saves the image in PNG format to the buffer
|
27 |
+
byte_im = buf.getvalue() # Retrieves the byte data of the image
|
28 |
+
return byte_im # Returns the byte representation of the image
|
29 |
+
|
30 |
+
# Function to process and display the uploaded image
|
31 |
+
def fix_image(upload):
|
32 |
+
image = Image.open(upload) # Opens the uploaded image using PIL
|
33 |
+
col1.write("Original Image :camera:") # Writes a label for the original image column
|
34 |
+
col1.image(image) # Displays the original image in the first column
|
35 |
+
|
36 |
+
fixed = remove(image) # Removes the background from the uploaded image
|
37 |
+
col2.write("Fixed Image :wrench:") # Writes a label for the processed image column
|
38 |
+
col2.image(fixed) # Displays the processed image in the second column
|
39 |
+
st.sidebar.markdown("\n") # Adds spacing in the sidebar
|
40 |
+
|
41 |
+
# Adds a download button in the sidebar to download the processed image
|
42 |
+
st.sidebar.download_button(
|
43 |
+
"Download fixed image", convert_image(fixed), "fixed.png", "image/png"
|
44 |
+
)
|
45 |
+
|
46 |
+
# Creating two columns to display original and processed images
|
47 |
+
col1, col2 = st.columns(2)
|
48 |
+
|
49 |
+
# Sidebar file uploader for uploading images
|
50 |
+
my_upload = st.sidebar.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
51 |
+
|
52 |
+
# Checking if a file is uploaded
|
53 |
+
if my_upload is not None:
|
54 |
+
# Validates the file size and processes it if it's within the limit
|
55 |
+
if my_upload.size > MAX_FILE_SIZE:
|
56 |
+
st.error("The uploaded file is too large. Please upload an image smaller than 5MB.") # Displays an error message for oversized files
|
57 |
+
else:
|
58 |
+
fix_image(upload=my_upload) # Processes and displays the uploaded image
|
59 |
+
else:
|
60 |
+
# Processes and displays a default image if no file is uploaded
|
61 |
+
fix_image("./elon musk.jpg")
|