ssh-AI commited on
Commit
dd99add
·
verified ·
1 Parent(s): e4cb210

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -33
app.py CHANGED
@@ -7,31 +7,31 @@ 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(
@@ -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 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
 
@@ -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
- 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
-
 
 
 
 
 
 
 
 
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