Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import uuid
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
#
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 os
|
| 8 |
|
| 9 |
+
# --- Custom Background with Semi-transparent Overlay ---
|
| 10 |
+
page_bg_img = '''
|
| 11 |
+
<style>
|
| 12 |
+
.stApp {
|
| 13 |
+
background: linear-gradient(rgba(255,255,255,0.7), rgba(255,255,255,0.7)),
|
| 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 |
+
background-position: center;
|
| 17 |
+
background-attachment: fixed;
|
| 18 |
+
}
|
| 19 |
+
h1, h2, h3, p, label, div {
|
| 20 |
+
color: #000000;
|
| 21 |
+
}
|
| 22 |
+
</style>
|
| 23 |
+
'''
|
| 24 |
+
st.markdown(page_bg_img, unsafe_allow_html=True)
|
| 25 |
|
| 26 |
+
# --- App Title ---
|
| 27 |
+
st.markdown("<h1 style='text-align: center; font-size: 50px;'>🧹✨ Clean Your Images & Create a Stunning PDF ✨🧹</h1>", unsafe_allow_html=True)
|
| 28 |
|
| 29 |
+
st.write("#### 📸 Supported formats: **jpg, jpeg, png, webp**")
|
| 30 |
+
|
| 31 |
+
# --- Upload Section ---
|
| 32 |
+
uploaded_files = st.file_uploader("Upload multiple images here", type=["jpg", "jpeg", "png", "webp"], accept_multiple_files=True)
|
| 33 |
+
|
| 34 |
+
# --- Functions ---
|
| 35 |
+
def clean_image(image_bytes):
|
| 36 |
+
# Remove background
|
| 37 |
+
img_no_bg = remove(image_bytes)
|
| 38 |
+
img = Image.open(io.BytesIO(img_no_bg)).convert("RGBA")
|
| 39 |
+
|
| 40 |
+
# Create white background
|
| 41 |
+
white_bg = Image.new("RGBA", img.size, (255, 255, 255, 255))
|
| 42 |
+
final_img = Image.alpha_composite(white_bg, img).convert("RGB")
|
| 43 |
+
|
| 44 |
+
return final_img
|
| 45 |
+
|
| 46 |
+
# --- Main Logic ---
|
| 47 |
+
if uploaded_files:
|
| 48 |
+
cleaned_images = []
|
| 49 |
+
with st.spinner('✨ Cleaning images... Please wait... ✨'):
|
| 50 |
+
for uploaded_file in uploaded_files:
|
| 51 |
+
img_bytes = uploaded_file.read()
|
| 52 |
+
cleaned_img = clean_image(img_bytes)
|
| 53 |
+
cleaned_images.append(cleaned_img)
|
| 54 |
+
|
| 55 |
+
if cleaned_images:
|
| 56 |
+
# Create a new PDF
|
| 57 |
+
pdf = FPDF(unit="mm", format="A4")
|
| 58 |
+
|
| 59 |
+
for img in cleaned_images:
|
| 60 |
+
# Save cleaned image temporarily
|
| 61 |
+
temp_path = f"/tmp/{uuid.uuid4()}.png"
|
| 62 |
+
img.save(temp_path)
|
| 63 |
+
|
| 64 |
+
# Add a new page
|
| 65 |
+
pdf.add_page()
|
| 66 |
+
|
| 67 |
+
# Calculate width/height for A4 size
|
| 68 |
+
pdf.image(temp_path, x=10, y=10, w=190) # Adjust image width
|
| 69 |
+
|
| 70 |
+
# Remove temporary file
|
| 71 |
+
os.remove(temp_path)
|
| 72 |
+
|
| 73 |
+
# Save final PDF
|
| 74 |
+
unique_filename = f"{uuid.uuid4()}.pdf"
|
| 75 |
+
pdf_path = f"/tmp/{unique_filename}"
|
| 76 |
+
pdf.output(pdf_path)
|
| 77 |
+
|
| 78 |
+
# Download Button
|
| 79 |
+
with open(pdf_path, "rb") as f:
|
| 80 |
+
st.success("✅ Images cleaned and combined in
|