Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from rembg import remove
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
import fitz # PyMuPDF for PDF handling
|
| 6 |
+
|
| 7 |
+
st.set_page_config(page_title="Background Remover", page_icon="🎨", layout="centered")
|
| 8 |
+
|
| 9 |
+
st.title("🎨 Background Remover")
|
| 10 |
+
st.markdown("Upload an image (PNG, JPEG, JPG, WEBP, or PDF) and remove the background instantly!")
|
| 11 |
+
|
| 12 |
+
# File uploader
|
| 13 |
+
uploaded_file = st.file_uploader("Upload an image or PDF", type=["png", "jpeg", "jpg", "webp", "pdf"])
|
| 14 |
+
|
| 15 |
+
if uploaded_file is not None:
|
| 16 |
+
file_type = uploaded_file.type
|
| 17 |
+
|
| 18 |
+
if file_type == "application/pdf":
|
| 19 |
+
# Convert PDF to image
|
| 20 |
+
doc = fitz.open(stream=uploaded_file.read(), filetype="pdf")
|
| 21 |
+
page = doc.load_page(0) # Only process the first page
|
| 22 |
+
image = Image.frombytes("RGB", [int(page.rect.width), int(page.rect.height)], page.get_pixmap().samples)
|
| 23 |
+
else:
|
| 24 |
+
image = Image.open(uploaded_file)
|
| 25 |
+
|
| 26 |
+
st.image(image, caption="Original Image", use_column_width=True)
|
| 27 |
+
|
| 28 |
+
# Remove background
|
| 29 |
+
with st.spinner("Removing background..."):
|
| 30 |
+
output_image = remove(image)
|
| 31 |
+
|
| 32 |
+
st.image(output_image, caption="Background Removed", use_column_width=True)
|
| 33 |
+
|
| 34 |
+
# Download button
|
| 35 |
+
img_byte_arr = io.BytesIO()
|
| 36 |
+
output_image.save(img_byte_arr, format="PNG")
|
| 37 |
+
st.download_button("Download Image", img_byte_arr.getvalue(), file_name="no_bg_image.png", mime="image/png")
|
| 38 |
+
|
| 39 |
+
st.markdown("---")
|
| 40 |
+
st.markdown("💡 Developed with ❤️ using Streamlit and rembg")
|