Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
if uploaded_files:
|
| 44 |
cleaned_images = []
|
| 45 |
-
|
| 46 |
for uploaded_file in uploaded_files:
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
buffered = io.BytesIO()
|
| 51 |
-
input_image.save(buffered, format="PNG")
|
| 52 |
-
buffered = buffered.getvalue()
|
| 53 |
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
|
| 70 |
-
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|