Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,74 +1,12 @@
|
|
| 1 |
-
import
|
| 2 |
-
from PIL import Image
|
| 3 |
-
import io
|
| 4 |
-
from rembg import remove
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
.stApp {
|
| 10 |
-
background-image: url("https://images.unsplash.com/photo-1587620962725-abab7fe55159?ixlib=rb-4.0.3&auto=format&fit=crop&w=1350&q=80");
|
| 11 |
-
background-size: cover;
|
| 12 |
-
background-position: center;
|
| 13 |
-
background-attachment: fixed;
|
| 14 |
-
}
|
| 15 |
-
h1, h2, h3 {
|
| 16 |
-
color: #ffffff;
|
| 17 |
-
text-shadow: 2px 2px 4px #000000;
|
| 18 |
-
}
|
| 19 |
-
</style>
|
| 20 |
-
'''
|
| 21 |
-
st.markdown(page_bg_img, unsafe_allow_html=True)
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
uploaded_files = st.file_uploader("Upload multiple images here", type=["jpg", "jpeg", "png", "webp"], accept_multiple_files=True)
|
| 30 |
-
|
| 31 |
-
# --- Functions ---
|
| 32 |
-
def clean_image(image_bytes):
|
| 33 |
-
# Remove background
|
| 34 |
-
img_no_bg = remove(image_bytes)
|
| 35 |
-
img = Image.open(io.BytesIO(img_no_bg)).convert("RGBA")
|
| 36 |
-
|
| 37 |
-
# Create white background
|
| 38 |
-
white_bg = Image.new("RGBA", img.size, (255, 255, 255, 255))
|
| 39 |
-
final_img = Image.alpha_composite(white_bg, img).convert("RGB")
|
| 40 |
-
|
| 41 |
-
return final_img
|
| 42 |
-
|
| 43 |
-
# --- Main Logic ---
|
| 44 |
-
if uploaded_files:
|
| 45 |
-
cleaned_images = []
|
| 46 |
-
with st.spinner('✨ Cleaning images... ✨'):
|
| 47 |
-
for uploaded_file in uploaded_files:
|
| 48 |
-
img_bytes = uploaded_file.read()
|
| 49 |
-
cleaned_img = clean_image(img_bytes)
|
| 50 |
-
cleaned_images.append(cleaned_img)
|
| 51 |
-
|
| 52 |
-
if cleaned_images:
|
| 53 |
-
# Save images into a high-quality PDF
|
| 54 |
-
pdf_bytes = io.BytesIO()
|
| 55 |
-
image_list = [img.convert('RGB') for img in cleaned_images] # Ensure correct mode
|
| 56 |
-
first_image = image_list.pop(0)
|
| 57 |
-
|
| 58 |
-
first_image.save(
|
| 59 |
-
pdf_bytes,
|
| 60 |
-
format="PDF",
|
| 61 |
-
save_all=True,
|
| 62 |
-
append_images=image_list,
|
| 63 |
-
resolution=300.0, # 300 DPI for quality
|
| 64 |
-
quality=100, # Maximum quality
|
| 65 |
-
)
|
| 66 |
-
pdf_bytes.seek(0)
|
| 67 |
-
|
| 68 |
-
st.success("✅ Images cleaned and combined into a high-quality PDF!")
|
| 69 |
-
st.download_button(
|
| 70 |
-
label="💾 Download Final PDF",
|
| 71 |
-
data=pdf_bytes,
|
| 72 |
-
file_name="cleaned_images.pdf",
|
| 73 |
-
mime="application/pdf"
|
| 74 |
-
)
|
|
|
|
| 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")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|