ssh-AI commited on
Commit
962a073
·
verified ·
1 Parent(s): 474327c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -18
app.py CHANGED
@@ -2,18 +2,18 @@
2
 
3
  import streamlit as st
4
  from PIL import Image, ImageEnhance
5
- import os
6
- import io
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 {
@@ -30,8 +30,8 @@ background: rgba(255, 255, 255, 0.9);
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(
@@ -44,18 +44,28 @@ if uploaded_files:
44
  cleaned_images = []
45
 
46
  for uploaded_file in uploaded_files:
47
- image = Image.open(uploaded_file).convert("RGB")
 
 
 
 
 
 
 
 
 
 
48
 
49
- # Light enhance image for better clarity
50
- enhancer = ImageEnhance.Brightness(image)
51
- image = enhancer.enhance(1.1) # Slight brightness
52
 
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
 
@@ -72,7 +82,7 @@ if uploaded_files:
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)
 
2
 
3
  import streamlit as st
4
  from PIL import Image, ImageEnhance
 
 
5
  from fpdf import FPDF
6
+ import io
7
+ import os
8
  import tempfile
9
  import base64
10
+ from rembg import remove
11
+ import datetime
12
 
13
  # Setup the app page
14
+ st.set_page_config(page_title="AI Image Cleaner & PDF Creator", page_icon="🧹", layout="wide")
15
 
16
+ # Light background
17
  page_bg_color = """
18
  <style>
19
  body {
 
30
  st.markdown(page_bg_color, unsafe_allow_html=True)
31
 
32
  # Attractive Heading
33
+ st.markdown("<h1 style='text-align: center; color: #333333;'>🧹 AI Image Background Cleaner & PDF Converter 🖼️</h1>", unsafe_allow_html=True)
34
+ st.markdown("<h4 style='text-align: center; color: #555555;'>Upload your images ➔ AI removes background ➔ Download crystal-clear PDF!</h4>", unsafe_allow_html=True)
35
 
36
  # File uploader
37
  uploaded_files = st.file_uploader(
 
44
  cleaned_images = []
45
 
46
  for uploaded_file in uploaded_files:
47
+ input_image = Image.open(uploaded_file).convert("RGB")
48
+
49
+ # Remove background using rembg AI
50
+ buffered = io.BytesIO()
51
+ input_image.save(buffered, format="PNG")
52
+ buffered = buffered.getvalue()
53
+
54
+ output = remove(buffered)
55
+
56
+ # Create cleaned image
57
+ cleaned_image = Image.open(io.BytesIO(output)).convert("RGBA")
58
 
59
+ # Put cleaned subject over white background
60
+ white_bg = Image.new("RGB", cleaned_image.size, (255, 255, 255))
61
+ white_bg.paste(cleaned_image, mask=cleaned_image.split()[3])
62
 
63
+ # Optional slight enhance
64
+ enhancer = ImageEnhance.Brightness(white_bg)
65
+ white_bg = enhancer.enhance(1.05)
66
 
67
+ enhancer = ImageEnhance.Contrast(white_bg)
68
+ white_bg = enhancer.enhance(1.1)
 
69
 
70
  cleaned_images.append(white_bg)
71
 
 
82
  pdf.add_page()
83
  pdf.image(img_path, x=0, y=0, w=cleaned_images[0].width, h=cleaned_images[0].height)
84
 
85
+ # Unique filename
86
  timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
87
  pdf_output_path = f"cleaned_images_{timestamp}.pdf"
88
  pdf.output(pdf_output_path)