-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
47 lines (36 loc) · 1.43 KB
/
app.py
File metadata and controls
47 lines (36 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import streamlit as st
from rembg import remove
import numpy as np
import cv2
from io import BytesIO
def remove_background(input_image):
# Use rembg to remove the background
output_data = remove(input_image.read())
# Convert the output data (bytes) into a numpy array
output_image = np.frombuffer(output_data, np.uint8)
# Decode the image
output_image = cv2.imdecode(output_image, cv2.IMREAD_UNCHANGED)
# Convert to PNG for transparent background
_, output_png = cv2.imencode('.png', output_image)
return output_png.tobytes()
def main():
st.title("Background Removal from Image")
st.write("Upload an image to remove its background")
# Upload image
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Show the original image
st.image(uploaded_file, caption="Uploaded Image", use_container_width=True)
# Remove background
output_image_bytes = remove_background(uploaded_file)
# Show the background removed image
st.image(output_image_bytes, caption="Background Removed Image", use_container_width=True)
# Provide a download button
st.download_button(
label="Download Image with Background Removed",
data=output_image_bytes,
file_name="output.png",
mime="image/png"
)
if __name__ == "__main__":
main()