Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,31 +7,31 @@ import io
|
|
| 7 |
from fpdf import FPDF
|
| 8 |
import uuid
|
| 9 |
import datetime
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
st.set_page_config(page_title="Image Cleaner & PDF Creator", page_icon="🖼️", layout="wide")
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
<style>
|
| 17 |
body {
|
| 18 |
-
background-
|
| 19 |
-
background-size: cover;
|
| 20 |
-
color: #FFFFFF;
|
| 21 |
}
|
| 22 |
[data-testid="stHeader"] {
|
| 23 |
-
background: rgba(
|
| 24 |
}
|
| 25 |
[data-testid="stSidebar"] {
|
| 26 |
-
background: rgba(255, 255, 255, 0.
|
| 27 |
}
|
| 28 |
</style>
|
| 29 |
"""
|
| 30 |
-
st.markdown(
|
| 31 |
|
| 32 |
# Attractive Heading
|
| 33 |
-
st.markdown("<h1 style='text-align: center; color: #
|
| 34 |
-
st.markdown("<h4 style='text-align: center; color: #
|
| 35 |
|
| 36 |
# File uploader
|
| 37 |
uploaded_files = st.file_uploader(
|
|
@@ -53,9 +53,9 @@ if uploaded_files:
|
|
| 53 |
enhancer = ImageEnhance.Contrast(image)
|
| 54 |
image = enhancer.enhance(1.2) # Slight contrast
|
| 55 |
|
| 56 |
-
# White background to replace
|
| 57 |
white_bg = Image.new("RGB", image.size, (255, 255, 255))
|
| 58 |
-
white_bg.paste(image
|
| 59 |
|
| 60 |
cleaned_images.append(white_bg)
|
| 61 |
|
|
@@ -63,27 +63,35 @@ if uploaded_files:
|
|
| 63 |
if cleaned_images:
|
| 64 |
pdf = FPDF(unit="pt", format=[cleaned_images[0].width, cleaned_images[0].height])
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
|
| 79 |
-
#
|
| 80 |
with open(pdf_output_path, "rb") as f:
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from fpdf import FPDF
|
| 8 |
import uuid
|
| 9 |
import datetime
|
| 10 |
+
import tempfile
|
| 11 |
+
import base64
|
| 12 |
|
| 13 |
+
# Setup the app page
|
| 14 |
st.set_page_config(page_title="Image Cleaner & PDF Creator", page_icon="🖼️", layout="wide")
|
| 15 |
|
| 16 |
+
# Background color (light gray/white)
|
| 17 |
+
page_bg_color = """
|
| 18 |
<style>
|
| 19 |
body {
|
| 20 |
+
background-color: #f5f5f5;
|
|
|
|
|
|
|
| 21 |
}
|
| 22 |
[data-testid="stHeader"] {
|
| 23 |
+
background: rgba(255, 255, 255, 0.8);
|
| 24 |
}
|
| 25 |
[data-testid="stSidebar"] {
|
| 26 |
+
background: rgba(255, 255, 255, 0.9);
|
| 27 |
}
|
| 28 |
</style>
|
| 29 |
"""
|
| 30 |
+
st.markdown(page_bg_color, unsafe_allow_html=True)
|
| 31 |
|
| 32 |
# Attractive Heading
|
| 33 |
+
st.markdown("<h1 style='text-align: center; color: #333333;'>✨ Image Background Cleaner & PDF Converter ✨</h1>", unsafe_allow_html=True)
|
| 34 |
+
st.markdown("<h4 style='text-align: center; color: #555555;'>Upload your images, clean them & get a crystal-clear PDF easily!</h4>", unsafe_allow_html=True)
|
| 35 |
|
| 36 |
# File uploader
|
| 37 |
uploaded_files = st.file_uploader(
|
|
|
|
| 53 |
enhancer = ImageEnhance.Contrast(image)
|
| 54 |
image = enhancer.enhance(1.2) # Slight contrast
|
| 55 |
|
| 56 |
+
# White background to replace any black background
|
| 57 |
white_bg = Image.new("RGB", image.size, (255, 255, 255))
|
| 58 |
+
white_bg.paste(image)
|
| 59 |
|
| 60 |
cleaned_images.append(white_bg)
|
| 61 |
|
|
|
|
| 63 |
if cleaned_images:
|
| 64 |
pdf = FPDF(unit="pt", format=[cleaned_images[0].width, cleaned_images[0].height])
|
| 65 |
|
| 66 |
+
# Temporary directory to save images
|
| 67 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 68 |
+
for idx, img in enumerate(cleaned_images):
|
| 69 |
+
img_path = os.path.join(temp_dir, f"img_{idx}.jpg")
|
| 70 |
+
img.save(img_path, format="JPEG")
|
| 71 |
|
| 72 |
+
pdf.add_page()
|
| 73 |
+
pdf.image(img_path, x=0, y=0, w=cleaned_images[0].width, h=cleaned_images[0].height)
|
| 74 |
|
| 75 |
+
# Unique file name with timestamp
|
| 76 |
+
timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
|
| 77 |
+
pdf_output_path = f"cleaned_images_{timestamp}.pdf"
|
| 78 |
+
pdf.output(pdf_output_path)
|
| 79 |
|
| 80 |
+
# Load the pdf for download button
|
| 81 |
with open(pdf_output_path, "rb") as f:
|
| 82 |
+
pdf_bytes = f.read()
|
| 83 |
+
b64_pdf = base64.b64encode(pdf_bytes).decode()
|
| 84 |
+
|
| 85 |
+
download_link = f"""
|
| 86 |
+
<a href="data:application/pdf;base64,{b64_pdf}" download="{pdf_output_path}"
|
| 87 |
+
style="
|
| 88 |
+
display:inline-block;
|
| 89 |
+
padding:12px 24px;
|
| 90 |
+
font-size:18px;
|
| 91 |
+
font-weight:bold;
|
| 92 |
+
color:white;
|
| 93 |
+
background: linear-gradient(135deg, #4CAF50 0%, #81C784 100%);
|
| 94 |
+
border-radius:10px;
|
| 95 |
+
text-decoration:none;
|
| 96 |
+
text-align:center;">
|
| 97 |
+
📄 D
|