File size: 1,486 Bytes
028671c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from rembg import remove
from PIL import Image
import io
import fitz  # PyMuPDF for PDF handling

st.set_page_config(page_title="Background Remover", page_icon="🎨", layout="centered")

st.title("🎨 Background Remover")
st.markdown("Upload an image (PNG, JPEG, JPG, WEBP, or PDF) and remove the background instantly!")

# File uploader
uploaded_file = st.file_uploader("Upload an image or PDF", type=["png", "jpeg", "jpg", "webp", "pdf"])

if uploaded_file is not None:
    file_type = uploaded_file.type
    
    if file_type == "application/pdf":
        # Convert PDF to image
        doc = fitz.open(stream=uploaded_file.read(), filetype="pdf")
        page = doc.load_page(0)  # Only process the first page
        image = Image.frombytes("RGB", [int(page.rect.width), int(page.rect.height)], page.get_pixmap().samples)
    else:
        image = Image.open(uploaded_file)
    
    st.image(image, caption="Original Image", use_column_width=True)
    
    # Remove background
    with st.spinner("Removing background..."):
        output_image = remove(image)
    
    st.image(output_image, caption="Background Removed", use_column_width=True)
    
    # Download button
    img_byte_arr = io.BytesIO()
    output_image.save(img_byte_arr, format="PNG")
    st.download_button("Download Image", img_byte_arr.getvalue(), file_name="no_bg_image.png", mime="image/png")

st.markdown("---")
st.markdown("💡 Developed with ❤️ using Streamlit and rembg")