Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,7 +18,6 @@ def resize_image(img, max_height=600):
|
|
| 18 |
def crop_sliders(img, key):
|
| 19 |
w, h = img.size
|
| 20 |
state = st.session_state
|
| 21 |
-
# Initialize default values
|
| 22 |
for name, val in zip(["left", "top", "right", "bottom"], [0, 0, w, h]):
|
| 23 |
state.setdefault(f"{key}_{name}", val)
|
| 24 |
|
|
@@ -30,7 +29,6 @@ def crop_sliders(img, key):
|
|
| 30 |
top = st.slider("Crop Top", 0, h - 10, state[f"{key}_top"], key=f"{key}_top_slider")
|
| 31 |
bottom = st.slider("Crop Bottom", top + 10, h, state[f"{key}_bottom"], key=f"{key}_bottom_slider")
|
| 32 |
|
| 33 |
-
# Store new values
|
| 34 |
state[f"{key}_left"] = left
|
| 35 |
state[f"{key}_top"] = top
|
| 36 |
state[f"{key}_right"] = right
|
|
@@ -54,12 +52,16 @@ if uploaded_files:
|
|
| 54 |
st.markdown(f"---\n### 🖼️ {file.name}")
|
| 55 |
original = Image.open(BytesIO(file_bytes)).convert("RGB")
|
| 56 |
|
| 57 |
-
# Init session state for this image
|
| 58 |
if f"original_{key}" not in st.session_state:
|
| 59 |
st.session_state[f"original_{key}"] = original
|
| 60 |
st.session_state[f"angle_{key}"] = 0
|
| 61 |
st.session_state[f"enhance_{key}"] = False
|
| 62 |
st.session_state[f"filename_{key}"] = file.name.rsplit(".", 1)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
if mode == "Normal Mode":
|
| 65 |
st.image(resize_image(original), use_container_width=True)
|
|
@@ -69,6 +71,7 @@ if uploaded_files:
|
|
| 69 |
|
| 70 |
col1, col2 = st.columns([1, 1.5])
|
| 71 |
with col1:
|
|
|
|
| 72 |
if st.button("🔁 Reset", key=f"reset_{key}"):
|
| 73 |
st.session_state[f"angle_{key}"] = 0
|
| 74 |
st.session_state[f"enhance_{key}"] = False
|
|
@@ -81,40 +84,39 @@ if uploaded_files:
|
|
| 81 |
st.slider("🌀 Rotate", -180, 180, st.session_state[f"angle_{key}"], key=f"angle_{key}")
|
| 82 |
st.checkbox("✨ Enhance", value=st.session_state[f"enhance_{key}"], key=f"enhance_{key}")
|
| 83 |
left, top, right, bottom = crop_sliders(original, key)
|
| 84 |
-
|
| 85 |
st.text_input("📁 Rename this file", value=st.session_state[f"filename_{key}"], key=f"filename_{key}_input")
|
| 86 |
|
| 87 |
-
if st.button("✅ Save This Image", key=f"save_{key}"):
|
| 88 |
-
st.session_state[f"final_{key}"] = st.session_state[f"preview_{key}"].copy()
|
| 89 |
-
st.success("Saved for export!")
|
| 90 |
-
|
| 91 |
with col2:
|
| 92 |
-
#
|
| 93 |
-
|
| 94 |
-
|
| 95 |
|
| 96 |
if st.session_state[f"enhance_{key}"]:
|
| 97 |
-
|
| 98 |
-
|
| 99 |
|
| 100 |
angle = st.session_state[f"angle_{key}"]
|
| 101 |
if angle != 0:
|
| 102 |
-
|
| 103 |
|
| 104 |
-
st.session_state[f"preview_{key}"] =
|
| 105 |
-
st.image(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
final_img = st.session_state.get(f"final_{key}", original)
|
| 108 |
filename = st.session_state.get(f"filename_{key}", file.name.rsplit(".", 1)[0])
|
| 109 |
processed_images.append(final_img)
|
| 110 |
file_names.append(filename)
|
| 111 |
|
| 112 |
-
# ---
|
| 113 |
st.markdown("### 🧾 Export Options")
|
| 114 |
with st.container():
|
| 115 |
-
|
| 116 |
-
with
|
| 117 |
-
if st.button("📦
|
| 118 |
zip_buffer = BytesIO()
|
| 119 |
with zipfile.ZipFile(zip_buffer, "w") as zipf:
|
| 120 |
for name, img in zip(file_names, processed_images):
|
|
@@ -122,10 +124,10 @@ if uploaded_files:
|
|
| 122 |
img.save(buffer, format="PNG")
|
| 123 |
zipf.writestr(f"{name}.png", buffer.getvalue())
|
| 124 |
zip_buffer.seek(0)
|
| 125 |
-
st.download_button("
|
| 126 |
|
| 127 |
-
with
|
| 128 |
-
if st.button("📄
|
| 129 |
pdf = FPDF()
|
| 130 |
tmp_paths = []
|
| 131 |
for name, img in zip(file_names, processed_images):
|
|
@@ -134,9 +136,9 @@ if uploaded_files:
|
|
| 134 |
pdf.add_page()
|
| 135 |
pdf.image(path, x=10, y=10, w=190)
|
| 136 |
tmp_paths.append(path)
|
| 137 |
-
|
| 138 |
-
pdf.output(
|
| 139 |
-
with open(
|
| 140 |
-
st.download_button("
|
| 141 |
-
for p in tmp_paths + [
|
| 142 |
os.remove(p)
|
|
|
|
| 18 |
def crop_sliders(img, key):
|
| 19 |
w, h = img.size
|
| 20 |
state = st.session_state
|
|
|
|
| 21 |
for name, val in zip(["left", "top", "right", "bottom"], [0, 0, w, h]):
|
| 22 |
state.setdefault(f"{key}_{name}", val)
|
| 23 |
|
|
|
|
| 29 |
top = st.slider("Crop Top", 0, h - 10, state[f"{key}_top"], key=f"{key}_top_slider")
|
| 30 |
bottom = st.slider("Crop Bottom", top + 10, h, state[f"{key}_bottom"], key=f"{key}_bottom_slider")
|
| 31 |
|
|
|
|
| 32 |
state[f"{key}_left"] = left
|
| 33 |
state[f"{key}_top"] = top
|
| 34 |
state[f"{key}_right"] = right
|
|
|
|
| 52 |
st.markdown(f"---\n### 🖼️ {file.name}")
|
| 53 |
original = Image.open(BytesIO(file_bytes)).convert("RGB")
|
| 54 |
|
|
|
|
| 55 |
if f"original_{key}" not in st.session_state:
|
| 56 |
st.session_state[f"original_{key}"] = original
|
| 57 |
st.session_state[f"angle_{key}"] = 0
|
| 58 |
st.session_state[f"enhance_{key}"] = False
|
| 59 |
st.session_state[f"filename_{key}"] = file.name.rsplit(".", 1)[0]
|
| 60 |
+
w, h = original.size
|
| 61 |
+
st.session_state[f"{key}_left"] = 0
|
| 62 |
+
st.session_state[f"{key}_top"] = 0
|
| 63 |
+
st.session_state[f"{key}_right"] = w
|
| 64 |
+
st.session_state[f"{key}_bottom"] = h
|
| 65 |
|
| 66 |
if mode == "Normal Mode":
|
| 67 |
st.image(resize_image(original), use_container_width=True)
|
|
|
|
| 71 |
|
| 72 |
col1, col2 = st.columns([1, 1.5])
|
| 73 |
with col1:
|
| 74 |
+
# Controls
|
| 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
|
|
|
|
| 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 |
+
# Real-time preview
|
| 91 |
+
img = st.session_state[f"original_{key}"].crop((left, top, right, bottom))
|
| 92 |
+
img = resize_image(img)
|
| 93 |
|
| 94 |
if st.session_state[f"enhance_{key}"]:
|
| 95 |
+
img = ImageEnhance.Contrast(img).enhance(1.5)
|
| 96 |
+
img = ImageEnhance.Sharpness(img).enhance(2.0)
|
| 97 |
|
| 98 |
angle = st.session_state[f"angle_{key}"]
|
| 99 |
if angle != 0:
|
| 100 |
+
img = img.rotate(angle, expand=True)
|
| 101 |
|
| 102 |
+
st.session_state[f"preview_{key}"] = img
|
| 103 |
+
st.image(img, caption="🎯 Live Preview", use_container_width=True)
|
| 104 |
+
|
| 105 |
+
if st.button("✅ Save This Image", key=f"save_{key}"):
|
| 106 |
+
st.session_state[f"final_{key}"] = img.copy()
|
| 107 |
+
st.success("Saved for export!")
|
| 108 |
|
| 109 |
final_img = st.session_state.get(f"final_{key}", original)
|
| 110 |
filename = st.session_state.get(f"filename_{key}", file.name.rsplit(".", 1)[0])
|
| 111 |
processed_images.append(final_img)
|
| 112 |
file_names.append(filename)
|
| 113 |
|
| 114 |
+
# --- Export Section ---
|
| 115 |
st.markdown("### 🧾 Export Options")
|
| 116 |
with st.container():
|
| 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):
|
|
|
|
| 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 |
+
with col2:
|
| 130 |
+
if st.button("📄 Generate PDF", key="pdf_btn"):
|
| 131 |
pdf = FPDF()
|
| 132 |
tmp_paths = []
|
| 133 |
for name, img in zip(file_names, processed_images):
|
|
|
|
| 136 |
pdf.add_page()
|
| 137 |
pdf.image(path, x=10, y=10, w=190)
|
| 138 |
tmp_paths.append(path)
|
| 139 |
+
out_path = "/tmp/final.pdf"
|
| 140 |
+
pdf.output(out_path)
|
| 141 |
+
with open(out_path, "rb") as f:
|
| 142 |
+
st.download_button("⬇️ Download PDF", f, "scanned.pdf", mime="application/pdf", key="pdf_dl")
|
| 143 |
+
for p in tmp_paths + [out_path]:
|
| 144 |
os.remove(p)
|