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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -91
app.py CHANGED
@@ -1,107 +1,91 @@
1
- # app.py
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 {
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;'>🧹 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(
38
- "Upload multiple images (PNG, JPG, JPEG formats supported)",
39
- accept_multiple_files=True,
40
- type=["png", "jpg", "jpeg"]
41
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  if uploaded_files:
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
 
72
- # Convert all images into a single PDF
73
- if cleaned_images:
74
- pdf = FPDF(unit="pt", format=[cleaned_images[0].width, cleaned_images[0].height])
 
 
75
 
76
- # Temporary directory to save images
77
- with tempfile.TemporaryDirectory() as temp_dir:
78
- for idx, img in enumerate(cleaned_images):
79
- img_path = os.path.join(temp_dir, f"img_{idx}.jpg")
80
- img.save(img_path, format="JPEG")
81
-
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)
89
-
90
- # Load the pdf for download button
91
- with open(pdf_output_path, "rb") as f:
92
- pdf_bytes = f.read()
93
- b64_pdf = base64.b64encode(pdf_bytes).decode()
94
-
95
- download_link = (
96
- '<a href="data:application/pdf;base64,' + b64_pdf + '" '
97
- 'download="' + pdf_output_path + '" '
98
- 'style="display:inline-block; padding:12px 24px; font-size:18px; font-weight:bold; '
99
- 'color:white; background: linear-gradient(135deg, #4CAF50 0%, #81C784 100%); '
100
- 'border-radius:10px; text-decoration:none; text-align:center;">'
101
- '📄 Download Cleaned PDF'
102
- '</a>'
103
- )
104
-
105
- st.markdown(download_link, unsafe_allow_html=True)
106
-
107
- st.success("✅ Images cleaned and combined into PDF successfully!")
 
 
 
1
  import streamlit as st
2
+ from PIL import Image, ImageEnhance, ImageFilter
3
+ import os
4
  from fpdf import FPDF
5
  import io
 
 
6
  import base64
7
+
8
+ # Function to clean the image
9
+ def clean_image(image):
10
+ # Convert to RGB
11
+ image = image.convert('RGB')
12
+
13
+ # Lighten the image
14
+ enhancer = ImageEnhance.Brightness(image)
15
+ image = enhancer.enhance(1.5)
16
+
17
+ # Sharpen the image
18
+ enhancer = ImageEnhance.Sharpness(image)
19
+ image = enhancer.enhance(2.0)
20
+
21
+ # Slight blur (optional)
22
+ image = image.filter(ImageFilter.GaussianBlur(radius=1))
23
+
24
+ return image
25
+
26
+ # Set background image using custom HTML
27
+ def set_background(image_url):
28
+ st.markdown(
29
+ f"""
30
+ <style>
31
+ .stApp {{
32
+ background-image: url("{image_url}");
33
+ background-attachment: fixed;
34
+ background-size: cover;
35
+ }}
36
+ </style>
37
+ """,
38
+ unsafe_allow_html=True
39
+ )
40
+
41
+ # Streamlit App
42
+ st.set_page_config(page_title="CNIC Cleaner", layout="centered")
43
+
44
+ # Set background image (example URL, you can replace)
45
+ background_url = "https://images.unsplash.com/photo-1564865888387-7a4c49fa3c39?ixlib=rb-4.0.3&auto=format&fit=crop&w=1950&q=80"
46
+ set_background(background_url)
47
+
48
+ st.title("🪪 CNIC/ID Cleaner and PDF Maker")
49
+ st.markdown("<h5 style='color:white;'>Upload your CNIC/ID images, we will clean them and generate a PDF for you.</h5>", unsafe_allow_html=True)
50
+
51
+ uploaded_files = st.file_uploader("Upload CNIC Images", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
52
 
53
  if uploaded_files:
54
  cleaned_images = []
55
+
56
  for uploaded_file in uploaded_files:
57
+ image = Image.open(uploaded_file)
58
+ cleaned = clean_image(image)
59
+ cleaned_images.append(cleaned)
 
 
 
60
 
61
+ st.success("✅ Images cleaned successfully!")
62
 
63
+ if st.button("📄 Generate PDF"):
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='PNG')
69
+ temp_buffer.seek(0)
70
 
71
+ temp_filename = "temp_img.png"
72
+ with open(temp_filename, "wb") as f:
73
+ f.write(temp_buffer.read())
74
 
75
+ pdf.add_page()
76
+ pdf.image(temp_filename, x=0, y=0, w=img.width, h=img.height)
77
+ os.remove(temp_filename)
78
 
79
+ output_pdf_path = "cleaned_cnic_output.pdf"
80
+ pdf.output(output_pdf_path)
81
 
82
+ with open(output_pdf_path, "rb") as f:
83
+ base64_pdf = base64.b64encode(f.read()).decode('utf-8')
84
 
85
+ download_link = f"""
86
+ <a href="data:application/octet-stream;base64,{base64_pdf}" download="cleaned_cnic_output.pdf">
87
+ 📥 <strong>Click here to download your cleaned PDF</strong>
88
+ </a>
89
+ """
90
 
91
+ st.markdown(download_link, unsafe_allow_html=True)