Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,13 +20,13 @@ def resize_image(img, max_width=800):
|
|
| 20 |
|
| 21 |
# --- HEADER ---
|
| 22 |
st.title("📄 Smart ID & Document Scanner")
|
| 23 |
-
st.
|
| 24 |
|
| 25 |
# --- Clear Session State on New Upload ---
|
| 26 |
if "uploaded_files_hash" not in st.session_state:
|
| 27 |
st.session_state.uploaded_files_hash = None
|
| 28 |
|
| 29 |
-
uploaded_files = st.file_uploader("Upload your Images", type=["png", "jpg", "jpeg"], accept_multiple_files=True)
|
| 30 |
|
| 31 |
if uploaded_files:
|
| 32 |
uploaded_files_names = [file.name for file in uploaded_files]
|
|
@@ -36,78 +36,80 @@ if uploaded_files:
|
|
| 36 |
st.session_state.clear()
|
| 37 |
st.session_state.uploaded_files_hash = current_hash
|
| 38 |
|
| 39 |
-
app_mode = st.radio("Select Mode:", ["Normal Mode (No Editing)", "Advanced Mode (Edit Each Image)"])
|
| 40 |
|
| 41 |
-
st.
|
| 42 |
processed_images = []
|
| 43 |
|
| 44 |
if app_mode == "Normal Mode (No Editing)":
|
| 45 |
for uploaded_file in uploaded_files:
|
| 46 |
img = Image.open(uploaded_file).convert("RGB")
|
| 47 |
-
img = resize_image(img)
|
| 48 |
processed_images.append(img)
|
| 49 |
st.image(img, caption=uploaded_file.name, use_container_width=True)
|
| 50 |
|
| 51 |
else: # Advanced Mode
|
| 52 |
for idx, uploaded_file in enumerate(uploaded_files):
|
| 53 |
-
st.subheader(f"Editing: {uploaded_file.name}")
|
| 54 |
|
| 55 |
original_img = Image.open(uploaded_file).convert("RGB")
|
| 56 |
original_img = resize_image(original_img)
|
| 57 |
|
| 58 |
-
# Initialize session states
|
| 59 |
if f"edited_{idx}" not in st.session_state:
|
| 60 |
st.session_state[f"edited_{idx}"] = original_img
|
| 61 |
st.session_state[f"cropper_{idx}"] = original_img.copy()
|
| 62 |
-
st.session_state[f"preview_{idx}"] = None
|
| 63 |
|
| 64 |
-
# --- Layout: 2 columns ---
|
| 65 |
col_cropper, col_preview = st.columns([1, 1])
|
| 66 |
|
| 67 |
with col_cropper:
|
| 68 |
-
st.
|
| 69 |
crop_output = st_cropper(
|
| 70 |
st.session_state[f"cropper_{idx}"],
|
| 71 |
realtime_update=False,
|
| 72 |
box_color='#FF9900',
|
| 73 |
-
aspect_ratio=None
|
|
|
|
|
|
|
|
|
|
| 74 |
)
|
| 75 |
|
| 76 |
if isinstance(crop_output, np.ndarray):
|
| 77 |
crop_output = Image.fromarray(crop_output)
|
| 78 |
|
| 79 |
-
if st.button(
|
| 80 |
st.session_state[f"preview_{idx}"] = crop_output
|
| 81 |
st.success("✅ Cropped! Now Edit Preview.")
|
| 82 |
|
| 83 |
with col_preview:
|
| 84 |
-
st.
|
| 85 |
if st.session_state[f"preview_{idx}"] is not None:
|
| 86 |
st.image(st.session_state[f"preview_{idx}"], caption=f"Preview: {uploaded_file.name}", use_container_width=True)
|
| 87 |
|
| 88 |
-
if st.button(
|
| 89 |
enhancer = ImageEnhance.Contrast(st.session_state[f"preview_{idx}"])
|
| 90 |
sharpener = ImageEnhance.Sharpness(enhancer.enhance(1.5))
|
| 91 |
st.session_state[f"preview_{idx}"] = sharpener.enhance(2.0)
|
| 92 |
|
| 93 |
-
angle = st.slider(
|
| 94 |
|
| 95 |
-
if st.button(
|
| 96 |
rotated_img = st.session_state[f"preview_{idx}"].rotate(angle, expand=True)
|
| 97 |
st.session_state[f"preview_{idx}"] = rotated_img
|
| 98 |
|
| 99 |
-
if st.button(
|
| 100 |
st.session_state[f"preview_{idx}"] = crop_output.copy()
|
| 101 |
|
| 102 |
-
if st.button(
|
| 103 |
st.session_state[f"edited_{idx}"] = st.session_state[f"preview_{idx}"].copy()
|
| 104 |
st.success("✅ Changes Applied!")
|
|
|
|
| 105 |
else:
|
| 106 |
-
st.info("Please crop an area first to enable editing.")
|
| 107 |
|
| 108 |
processed_images.append(st.session_state.get(f"edited_{idx}", original_img))
|
| 109 |
|
| 110 |
-
st.
|
| 111 |
|
| 112 |
# --- EXPORT OPTIONS ---
|
| 113 |
st.header("📥 Download Options")
|
|
@@ -115,7 +117,7 @@ if uploaded_files:
|
|
| 115 |
col1, col2 = st.columns(2)
|
| 116 |
|
| 117 |
with col1:
|
| 118 |
-
if st.button("Download All as ZIP
|
| 119 |
with st.spinner("Creating ZIP..."):
|
| 120 |
zip_buffer = BytesIO()
|
| 121 |
with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED) as zip_file:
|
|
@@ -128,7 +130,7 @@ if uploaded_files:
|
|
| 128 |
st.download_button(label="📥 Download ZIP", data=zip_buffer, file_name="images.zip", mime="application/zip")
|
| 129 |
|
| 130 |
with col2:
|
| 131 |
-
if st.button("Download All as PDF
|
| 132 |
with st.spinner("Generating PDF..."):
|
| 133 |
pdf = FPDF()
|
| 134 |
temp_image_paths = []
|
|
|
|
| 20 |
|
| 21 |
# --- HEADER ---
|
| 22 |
st.title("📄 Smart ID & Document Scanner")
|
| 23 |
+
st.markdown("---")
|
| 24 |
|
| 25 |
# --- Clear Session State on New Upload ---
|
| 26 |
if "uploaded_files_hash" not in st.session_state:
|
| 27 |
st.session_state.uploaded_files_hash = None
|
| 28 |
|
| 29 |
+
uploaded_files = st.file_uploader("📤 Upload your Images", type=["png", "jpg", "jpeg"], accept_multiple_files=True)
|
| 30 |
|
| 31 |
if uploaded_files:
|
| 32 |
uploaded_files_names = [file.name for file in uploaded_files]
|
|
|
|
| 36 |
st.session_state.clear()
|
| 37 |
st.session_state.uploaded_files_hash = current_hash
|
| 38 |
|
| 39 |
+
app_mode = st.radio("🛠️ Select Mode:", ["Normal Mode (No Editing)", "Advanced Mode (Edit Each Image)"])
|
| 40 |
|
| 41 |
+
st.markdown("---")
|
| 42 |
processed_images = []
|
| 43 |
|
| 44 |
if app_mode == "Normal Mode (No Editing)":
|
| 45 |
for uploaded_file in uploaded_files:
|
| 46 |
img = Image.open(uploaded_file).convert("RGB")
|
| 47 |
+
img = resize_image(img)
|
| 48 |
processed_images.append(img)
|
| 49 |
st.image(img, caption=uploaded_file.name, use_container_width=True)
|
| 50 |
|
| 51 |
else: # Advanced Mode
|
| 52 |
for idx, uploaded_file in enumerate(uploaded_files):
|
| 53 |
+
st.subheader(f"🖼️ Editing: {uploaded_file.name}")
|
| 54 |
|
| 55 |
original_img = Image.open(uploaded_file).convert("RGB")
|
| 56 |
original_img = resize_image(original_img)
|
| 57 |
|
|
|
|
| 58 |
if f"edited_{idx}" not in st.session_state:
|
| 59 |
st.session_state[f"edited_{idx}"] = original_img
|
| 60 |
st.session_state[f"cropper_{idx}"] = original_img.copy()
|
| 61 |
+
st.session_state[f"preview_{idx}"] = None
|
| 62 |
|
|
|
|
| 63 |
col_cropper, col_preview = st.columns([1, 1])
|
| 64 |
|
| 65 |
with col_cropper:
|
| 66 |
+
st.markdown("### ✂️ Select Crop Area (Original Image):")
|
| 67 |
crop_output = st_cropper(
|
| 68 |
st.session_state[f"cropper_{idx}"],
|
| 69 |
realtime_update=False,
|
| 70 |
box_color='#FF9900',
|
| 71 |
+
aspect_ratio=None,
|
| 72 |
+
return_type="image",
|
| 73 |
+
fit_image=True,
|
| 74 |
+
container_height=600
|
| 75 |
)
|
| 76 |
|
| 77 |
if isinstance(crop_output, np.ndarray):
|
| 78 |
crop_output = Image.fromarray(crop_output)
|
| 79 |
|
| 80 |
+
if st.button("✂️ Crop Now", key=f"crop_{idx}"):
|
| 81 |
st.session_state[f"preview_{idx}"] = crop_output
|
| 82 |
st.success("✅ Cropped! Now Edit Preview.")
|
| 83 |
|
| 84 |
with col_preview:
|
| 85 |
+
st.markdown("### 🎯 Preview and Edit:")
|
| 86 |
if st.session_state[f"preview_{idx}"] is not None:
|
| 87 |
st.image(st.session_state[f"preview_{idx}"], caption=f"Preview: {uploaded_file.name}", use_container_width=True)
|
| 88 |
|
| 89 |
+
if st.button("✨ Enhance Image", key=f"enhance_{idx}"):
|
| 90 |
enhancer = ImageEnhance.Contrast(st.session_state[f"preview_{idx}"])
|
| 91 |
sharpener = ImageEnhance.Sharpness(enhancer.enhance(1.5))
|
| 92 |
st.session_state[f"preview_{idx}"] = sharpener.enhance(2.0)
|
| 93 |
|
| 94 |
+
angle = st.slider("🌀 Rotate Angle", -180, 180, 0, key=f"rotate_slider_{idx}")
|
| 95 |
|
| 96 |
+
if st.button("🔄 Rotate Image", key=f"rotate_{idx}"):
|
| 97 |
rotated_img = st.session_state[f"preview_{idx}"].rotate(angle, expand=True)
|
| 98 |
st.session_state[f"preview_{idx}"] = rotated_img
|
| 99 |
|
| 100 |
+
if st.button("🗑️ Reset Preview", key=f"reset_preview_{idx}"):
|
| 101 |
st.session_state[f"preview_{idx}"] = crop_output.copy()
|
| 102 |
|
| 103 |
+
if st.button("✅ Apply Changes", key=f"apply_{idx}"):
|
| 104 |
st.session_state[f"edited_{idx}"] = st.session_state[f"preview_{idx}"].copy()
|
| 105 |
st.success("✅ Changes Applied!")
|
| 106 |
+
|
| 107 |
else:
|
| 108 |
+
st.info("✂️ Please crop an area first to enable editing.")
|
| 109 |
|
| 110 |
processed_images.append(st.session_state.get(f"edited_{idx}", original_img))
|
| 111 |
|
| 112 |
+
st.markdown("---")
|
| 113 |
|
| 114 |
# --- EXPORT OPTIONS ---
|
| 115 |
st.header("📥 Download Options")
|
|
|
|
| 117 |
col1, col2 = st.columns(2)
|
| 118 |
|
| 119 |
with col1:
|
| 120 |
+
if st.button("📸 Download All as ZIP"):
|
| 121 |
with st.spinner("Creating ZIP..."):
|
| 122 |
zip_buffer = BytesIO()
|
| 123 |
with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED) as zip_file:
|
|
|
|
| 130 |
st.download_button(label="📥 Download ZIP", data=zip_buffer, file_name="images.zip", mime="application/zip")
|
| 131 |
|
| 132 |
with col2:
|
| 133 |
+
if st.button("📄 Download All as PDF"):
|
| 134 |
with st.spinner("Generating PDF..."):
|
| 135 |
pdf = FPDF()
|
| 136 |
temp_image_paths = []
|