Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -71,7 +71,7 @@ if uploaded_files:
|
|
| 71 |
|
| 72 |
col1, col2 = st.columns([1, 1.5])
|
| 73 |
with col1:
|
| 74 |
-
#
|
| 75 |
if st.button("🔁 Reset", key=f"reset_{key}"):
|
| 76 |
st.session_state[f"angle_{key}"] = 0
|
| 77 |
st.session_state[f"enhance_{key}"] = False
|
|
@@ -81,29 +81,30 @@ if uploaded_files:
|
|
| 81 |
st.session_state[f"{key}_right"] = w
|
| 82 |
st.session_state[f"{key}_bottom"] = h
|
| 83 |
|
|
|
|
| 84 |
st.slider("🌀 Rotate", -180, 180, st.session_state[f"angle_{key}"], key=f"angle_{key}")
|
| 85 |
st.checkbox("✨ Enhance", value=st.session_state[f"enhance_{key}"], key=f"enhance_{key}")
|
| 86 |
left, top, right, bottom = crop_sliders(original, key)
|
| 87 |
st.text_input("📁 Rename this file", value=st.session_state[f"filename_{key}"], key=f"filename_{key}_input")
|
| 88 |
|
| 89 |
with col2:
|
| 90 |
-
#
|
| 91 |
-
|
| 92 |
-
|
| 93 |
|
| 94 |
if st.session_state[f"enhance_{key}"]:
|
| 95 |
-
|
| 96 |
-
|
| 97 |
|
| 98 |
angle = st.session_state[f"angle_{key}"]
|
| 99 |
if angle != 0:
|
| 100 |
-
|
| 101 |
|
| 102 |
-
st.session_state[f"preview_{key}"] =
|
| 103 |
-
st.image(
|
| 104 |
|
| 105 |
if st.button("✅ Save This Image", key=f"save_{key}"):
|
| 106 |
-
st.session_state[f"final_{key}"] =
|
| 107 |
st.success("Saved for export!")
|
| 108 |
|
| 109 |
final_img = st.session_state.get(f"final_{key}", original)
|
|
@@ -111,34 +112,45 @@ if uploaded_files:
|
|
| 111 |
processed_images.append(final_img)
|
| 112 |
file_names.append(filename)
|
| 113 |
|
| 114 |
-
# ---
|
| 115 |
st.markdown("### 🧾 Export Options")
|
| 116 |
-
|
| 117 |
-
col1, col2 = st.columns([1, 1])
|
| 118 |
-
with col1:
|
| 119 |
-
if st.button("📦 Generate ZIP", key="zip_btn"):
|
| 120 |
-
zip_buffer = BytesIO()
|
| 121 |
-
with zipfile.ZipFile(zip_buffer, "w") as zipf:
|
| 122 |
-
for name, img in zip(file_names, processed_images):
|
| 123 |
-
buffer = BytesIO()
|
| 124 |
-
img.save(buffer, format="PNG")
|
| 125 |
-
zipf.writestr(f"{name}.png", buffer.getvalue())
|
| 126 |
-
zip_buffer.seek(0)
|
| 127 |
-
st.download_button("⬇️ Download ZIP", zip_buffer, "images.zip", mime="application/zip", key="zip_dl")
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
| 133 |
for name, img in zip(file_names, processed_images):
|
| 134 |
-
|
| 135 |
-
img.save(
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
col1, col2 = st.columns([1, 1.5])
|
| 73 |
with col1:
|
| 74 |
+
# Reset button
|
| 75 |
if st.button("🔁 Reset", key=f"reset_{key}"):
|
| 76 |
st.session_state[f"angle_{key}"] = 0
|
| 77 |
st.session_state[f"enhance_{key}"] = False
|
|
|
|
| 81 |
st.session_state[f"{key}_right"] = w
|
| 82 |
st.session_state[f"{key}_bottom"] = h
|
| 83 |
|
| 84 |
+
# Controls
|
| 85 |
st.slider("🌀 Rotate", -180, 180, st.session_state[f"angle_{key}"], key=f"angle_{key}")
|
| 86 |
st.checkbox("✨ Enhance", value=st.session_state[f"enhance_{key}"], key=f"enhance_{key}")
|
| 87 |
left, top, right, bottom = crop_sliders(original, key)
|
| 88 |
st.text_input("📁 Rename this file", value=st.session_state[f"filename_{key}"], key=f"filename_{key}_input")
|
| 89 |
|
| 90 |
with col2:
|
| 91 |
+
# Live preview logic here (always runs)
|
| 92 |
+
preview = st.session_state[f"original_{key}"].crop((left, top, right, bottom))
|
| 93 |
+
preview = resize_image(preview)
|
| 94 |
|
| 95 |
if st.session_state[f"enhance_{key}"]:
|
| 96 |
+
preview = ImageEnhance.Contrast(preview).enhance(1.5)
|
| 97 |
+
preview = ImageEnhance.Sharpness(preview).enhance(2.0)
|
| 98 |
|
| 99 |
angle = st.session_state[f"angle_{key}"]
|
| 100 |
if angle != 0:
|
| 101 |
+
preview = preview.rotate(angle, expand=True)
|
| 102 |
|
| 103 |
+
st.session_state[f"preview_{key}"] = preview
|
| 104 |
+
st.image(preview, caption="🎯 Live Preview", use_container_width=True)
|
| 105 |
|
| 106 |
if st.button("✅ Save This Image", key=f"save_{key}"):
|
| 107 |
+
st.session_state[f"final_{key}"] = preview.copy()
|
| 108 |
st.success("Saved for export!")
|
| 109 |
|
| 110 |
final_img = st.session_state.get(f"final_{key}", original)
|
|
|
|
| 112 |
processed_images.append(final_img)
|
| 113 |
file_names.append(filename)
|
| 114 |
|
| 115 |
+
# --- EXPORT SECTION ---
|
| 116 |
st.markdown("### 🧾 Export Options")
|
| 117 |
+
col1, col2 = st.columns([1, 1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
show_zip = st.session_state.get("show_zip", False)
|
| 120 |
+
show_pdf = st.session_state.get("show_pdf", False)
|
| 121 |
+
|
| 122 |
+
with col1:
|
| 123 |
+
if st.button("📦 Generate ZIP"):
|
| 124 |
+
zip_buffer = BytesIO()
|
| 125 |
+
with zipfile.ZipFile(zip_buffer, "w") as zipf:
|
| 126 |
for name, img in zip(file_names, processed_images):
|
| 127 |
+
buffer = BytesIO()
|
| 128 |
+
img.save(buffer, format="PNG")
|
| 129 |
+
zipf.writestr(f"{name}.png", buffer.getvalue())
|
| 130 |
+
zip_buffer.seek(0)
|
| 131 |
+
st.session_state["zip_file"] = zip_buffer
|
| 132 |
+
st.session_state["show_zip"] = True
|
| 133 |
+
|
| 134 |
+
if st.session_state.get("show_zip", False):
|
| 135 |
+
st.download_button("⬇️ Download ZIP", st.session_state["zip_file"], "images.zip", mime="application/zip")
|
| 136 |
+
|
| 137 |
+
with col2:
|
| 138 |
+
if st.button("📄 Generate PDF"):
|
| 139 |
+
pdf = FPDF()
|
| 140 |
+
tmp_paths = []
|
| 141 |
+
for name, img in zip(file_names, processed_images):
|
| 142 |
+
path = f"/tmp/{name}.jpg"
|
| 143 |
+
img.save(path, "JPEG")
|
| 144 |
+
pdf.add_page()
|
| 145 |
+
pdf.image(path, x=10, y=10, w=190)
|
| 146 |
+
tmp_paths.append(path)
|
| 147 |
+
out_path = "/tmp/final.pdf"
|
| 148 |
+
pdf.output(out_path)
|
| 149 |
+
with open(out_path, "rb") as f:
|
| 150 |
+
st.session_state["pdf_file"] = f.read()
|
| 151 |
+
for p in tmp_paths + [out_path]:
|
| 152 |
+
os.remove(p)
|
| 153 |
+
st.session_state["show_pdf"] = True
|
| 154 |
+
|
| 155 |
+
if st.session_state.get("show_pdf", False):
|
| 156 |
+
st.download_button("⬇️ Download PDF", st.session_state["pdf_file"], "scanned.pdf", mime="application/pdf")
|