ssh-AI commited on
Commit
21ad2d2
·
verified ·
1 Parent(s): fa8ac4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -8
app.py CHANGED
@@ -1,12 +1,80 @@
 
 
 
1
  import uuid
 
 
 
2
 
3
- # In your PDF generation function
4
- unique_filename = f"{uuid.uuid4()}.pdf" # This generates a random unique filename
5
- pdf_output_path = f"/tmp/{unique_filename}"
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- # Save the PDF with this path
8
- pdf.output(pdf_output_path)
9
 
10
- # Provide this file to the user for download
11
- with open(pdf_output_path, "rb") as f:
12
- st.download_button("Download PDF", f, file_name="enhanced_images.pdf", mime="application/pdf")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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