Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,90 +1,89 @@
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from PIL import Image
|
|
|
|
| 3 |
import io
|
| 4 |
-
import uuid
|
| 5 |
-
from rembg import remove
|
| 6 |
from fpdf import FPDF
|
| 7 |
-
import
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
| 11 |
<style>
|
| 12 |
-
|
| 13 |
-
background:
|
| 14 |
-
url("https://images.unsplash.com/photo-1587620962725-abab7fe55159?ixlib=rb-4.0.3&auto=format&fit=crop&w=1350&q=80");
|
| 15 |
background-size: cover;
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
}
|
| 19 |
-
|
| 20 |
-
|
| 21 |
}
|
| 22 |
</style>
|
| 23 |
-
|
| 24 |
st.markdown(page_bg_img, unsafe_allow_html=True)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
st.markdown("<h1 style='text-align: center;
|
| 28 |
-
|
| 29 |
-
st.write("#### 📸 Supported formats: **jpg, jpeg, png, webp**")
|
| 30 |
|
| 31 |
-
#
|
| 32 |
uploaded_files = st.file_uploader(
|
| 33 |
-
"Upload multiple images
|
| 34 |
-
|
| 35 |
-
|
| 36 |
)
|
| 37 |
|
| 38 |
-
# --- Functions ---
|
| 39 |
-
def clean_image(image_bytes):
|
| 40 |
-
# Remove background
|
| 41 |
-
img_no_bg = remove(image_bytes)
|
| 42 |
-
img = Image.open(io.BytesIO(img_no_bg)).convert("RGBA")
|
| 43 |
-
|
| 44 |
-
# Create white background
|
| 45 |
-
white_bg = Image.new("RGBA", img.size, (255, 255, 255, 255))
|
| 46 |
-
final_img = Image.alpha_composite(white_bg, img).convert("RGB")
|
| 47 |
-
|
| 48 |
-
return final_img
|
| 49 |
-
|
| 50 |
-
# --- Main Logic ---
|
| 51 |
if uploaded_files:
|
| 52 |
cleaned_images = []
|
| 53 |
-
with st.spinner('✨ Cleaning images... Please wait... ✨'):
|
| 54 |
-
for uploaded_file in uploaded_files:
|
| 55 |
-
img_bytes = uploaded_file.read()
|
| 56 |
-
cleaned_img = clean_image(img_bytes)
|
| 57 |
-
cleaned_images.append(cleaned_img)
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
if cleaned_images:
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
for img in cleaned_images:
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
# Add a new page
|
| 69 |
pdf.add_page()
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
#
|
| 78 |
-
|
| 79 |
-
pdf_path = f"/tmp/{unique_filename}"
|
| 80 |
-
pdf.output(pdf_path)
|
| 81 |
-
|
| 82 |
-
# Download Button
|
| 83 |
-
with open(pdf_path, "rb") as f:
|
| 84 |
-
st.success("✅ Images cleaned and combined into a high-quality PDF!")
|
| 85 |
st.download_button(
|
| 86 |
-
label="
|
| 87 |
data=f,
|
| 88 |
-
file_name=
|
| 89 |
mime="application/pdf"
|
| 90 |
)
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
import streamlit as st
|
| 4 |
+
from PIL import Image, ImageEnhance
|
| 5 |
+
import os
|
| 6 |
import io
|
|
|
|
|
|
|
| 7 |
from fpdf import FPDF
|
| 8 |
+
import uuid
|
| 9 |
+
import datetime
|
| 10 |
|
| 11 |
+
# App page setup
|
| 12 |
+
st.set_page_config(page_title="Image Cleaner & PDF Creator", page_icon="🖼️", layout="wide")
|
| 13 |
+
|
| 14 |
+
# Set background color and slight image
|
| 15 |
+
page_bg_img = """
|
| 16 |
<style>
|
| 17 |
+
body {
|
| 18 |
+
background-image: url('https://images.unsplash.com/photo-1542281286-9e0a16bb7366');
|
|
|
|
| 19 |
background-size: cover;
|
| 20 |
+
color: #FFFFFF;
|
| 21 |
+
}
|
| 22 |
+
[data-testid="stHeader"] {
|
| 23 |
+
background: rgba(0,0,0,0);
|
| 24 |
}
|
| 25 |
+
[data-testid="stSidebar"] {
|
| 26 |
+
background: rgba(255, 255, 255, 0.1);
|
| 27 |
}
|
| 28 |
</style>
|
| 29 |
+
"""
|
| 30 |
st.markdown(page_bg_img, unsafe_allow_html=True)
|
| 31 |
|
| 32 |
+
# Attractive Heading
|
| 33 |
+
st.markdown("<h1 style='text-align: center; color: #FFFFFF;'>✨ Image Background Cleaner & PDF Converter ✨</h1>", unsafe_allow_html=True)
|
| 34 |
+
st.markdown("<h4 style='text-align: center; color: #CCCCCC;'>Upload your images, clean backgrounds & download a single combined PDF</h4>", unsafe_allow_html=True)
|
|
|
|
| 35 |
|
| 36 |
+
# File uploader
|
| 37 |
uploaded_files = st.file_uploader(
|
| 38 |
+
"Upload multiple images (PNG, JPG, JPEG formats supported)",
|
| 39 |
+
accept_multiple_files=True,
|
| 40 |
+
type=["png", "jpg", "jpeg"]
|
| 41 |
)
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
if uploaded_files:
|
| 44 |
cleaned_images = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
for uploaded_file in uploaded_files:
|
| 47 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 48 |
+
|
| 49 |
+
# Light enhance image for better clarity
|
| 50 |
+
enhancer = ImageEnhance.Brightness(image)
|
| 51 |
+
image = enhancer.enhance(1.1) # Slight brightness
|
| 52 |
+
|
| 53 |
+
enhancer = ImageEnhance.Contrast(image)
|
| 54 |
+
image = enhancer.enhance(1.2) # Slight contrast
|
| 55 |
+
|
| 56 |
+
# White background to replace transparent or black
|
| 57 |
+
white_bg = Image.new("RGB", image.size, (255, 255, 255))
|
| 58 |
+
white_bg.paste(image, (0, 0), mask=None)
|
| 59 |
+
|
| 60 |
+
cleaned_images.append(white_bg)
|
| 61 |
+
|
| 62 |
+
# Convert all images into a single PDF
|
| 63 |
if cleaned_images:
|
| 64 |
+
pdf = FPDF(unit="pt", format=[cleaned_images[0].width, cleaned_images[0].height])
|
| 65 |
+
|
|
|
|
| 66 |
for img in cleaned_images:
|
| 67 |
+
temp_buffer = io.BytesIO()
|
| 68 |
+
img.save(temp_buffer, format="JPEG")
|
| 69 |
+
temp_buffer.seek(0)
|
| 70 |
+
|
|
|
|
| 71 |
pdf.add_page()
|
| 72 |
+
pdf.image(temp_buffer, x=0, y=0, w=cleaned_images[0].width, h=cleaned_images[0].height)
|
| 73 |
+
|
| 74 |
+
# Unique file name with timestamp
|
| 75 |
+
timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
|
| 76 |
+
pdf_output_path = f"cleaned_images_{timestamp}.pdf"
|
| 77 |
+
pdf.output(pdf_output_path)
|
| 78 |
+
|
| 79 |
+
# Download button
|
| 80 |
+
with open(pdf_output_path, "rb") as f:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
st.download_button(
|
| 82 |
+
label="📄 Download Cleaned PDF",
|
| 83 |
data=f,
|
| 84 |
+
file_name=pdf_output_path,
|
| 85 |
mime="application/pdf"
|
| 86 |
)
|
| 87 |
+
|
| 88 |
+
st.success("✅ Images cleaned and combined into PDF successfully!")
|
| 89 |
+
|