Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,16 @@ import numpy as np
|
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
|
| 7 |
-
st.title(" Image Augmentation Tool (Multi-image Support)")
|
| 8 |
|
| 9 |
-
uploaded_files = st.file_uploader(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
if uploaded_files:
|
| 12 |
-
st.sidebar.title("Transformations")
|
| 13 |
|
| 14 |
option = st.sidebar.selectbox("Choose a transformation", (
|
| 15 |
"None",
|
|
@@ -29,8 +33,6 @@ if uploaded_files:
|
|
| 29 |
image = Image.open(uploaded_file)
|
| 30 |
img_array = np.array(image)
|
| 31 |
|
| 32 |
-
st.image(image, caption="Original Image", use_column_width=True)
|
| 33 |
-
|
| 34 |
transformed_img = img_array.copy()
|
| 35 |
|
| 36 |
try:
|
|
@@ -51,26 +53,36 @@ if uploaded_files:
|
|
| 51 |
transformed_img = cv2.warpAffine(transformed_img, M, (transformed_img.shape[1], transformed_img.shape[0]))
|
| 52 |
elif option == "Crop (center 50%)":
|
| 53 |
h, w = transformed_img.shape[:2]
|
| 54 |
-
start_x = w//4
|
| 55 |
-
start_y = h//4
|
| 56 |
-
transformed_img = transformed_img[start_y:3*start_y, start_x:3*start_x]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
else:
|
| 62 |
-
st.
|
| 63 |
|
| 64 |
-
# Download
|
| 65 |
-
result = Image.fromarray(transformed_img)
|
| 66 |
-
buf = io.BytesIO()
|
| 67 |
-
result.save(buf, format="PNG")
|
| 68 |
-
byte_im = buf.getvalue()
|
| 69 |
-
st.download_button(
|
| 70 |
-
label=f"Download {uploaded_file.name}",
|
| 71 |
-
data=byte_im,
|
| 72 |
-
file_name=f"{uploaded_file.name.split('.')[0]}_transformed.png",
|
| 73 |
-
mime="image/png"
|
| 74 |
-
)
|
| 75 |
except Exception as e:
|
| 76 |
st.error(f"❌ Error with {uploaded_file.name}: {e}")
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
|
| 7 |
+
st.title("🖼️ Image Augmentation Tool (Multi-image Support)")
|
| 8 |
|
| 9 |
+
uploaded_files = st.file_uploader(
|
| 10 |
+
"Upload one or more images",
|
| 11 |
+
type=["jpg", "jpeg", "png"],
|
| 12 |
+
accept_multiple_files=True
|
| 13 |
+
)
|
| 14 |
|
| 15 |
if uploaded_files:
|
| 16 |
+
st.sidebar.title("🔧 Transformations")
|
| 17 |
|
| 18 |
option = st.sidebar.selectbox("Choose a transformation", (
|
| 19 |
"None",
|
|
|
|
| 33 |
image = Image.open(uploaded_file)
|
| 34 |
img_array = np.array(image)
|
| 35 |
|
|
|
|
|
|
|
| 36 |
transformed_img = img_array.copy()
|
| 37 |
|
| 38 |
try:
|
|
|
|
| 53 |
transformed_img = cv2.warpAffine(transformed_img, M, (transformed_img.shape[1], transformed_img.shape[0]))
|
| 54 |
elif option == "Crop (center 50%)":
|
| 55 |
h, w = transformed_img.shape[:2]
|
| 56 |
+
start_x = w // 4
|
| 57 |
+
start_y = h // 4
|
| 58 |
+
transformed_img = transformed_img[start_y:3 * start_y, start_x:3 * start_x]
|
| 59 |
+
|
| 60 |
+
# Display images side-by-side
|
| 61 |
+
col1, col2 = st.columns(2)
|
| 62 |
+
|
| 63 |
+
with col1:
|
| 64 |
+
st.image(image, caption="Original Image", use_column_width=True)
|
| 65 |
+
|
| 66 |
+
with col2:
|
| 67 |
+
if option != "None":
|
| 68 |
+
if len(transformed_img.shape) == 2:
|
| 69 |
+
st.image(transformed_img, caption=f"Transformed: {option}", use_column_width=True, channels="GRAY")
|
| 70 |
+
else:
|
| 71 |
+
st.image(transformed_img, caption=f"Transformed: {option}", use_column_width=True)
|
| 72 |
|
| 73 |
+
# Download button
|
| 74 |
+
result = Image.fromarray(transformed_img)
|
| 75 |
+
buf = io.BytesIO()
|
| 76 |
+
result.save(buf, format="PNG")
|
| 77 |
+
byte_im = buf.getvalue()
|
| 78 |
+
st.download_button(
|
| 79 |
+
label=f"⬇️ Download {uploaded_file.name}",
|
| 80 |
+
data=byte_im,
|
| 81 |
+
file_name=f"{uploaded_file.name.split('.')[0]}_transformed.png",
|
| 82 |
+
mime="image/png"
|
| 83 |
+
)
|
| 84 |
else:
|
| 85 |
+
st.info("No transformation selected.")
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
except Exception as e:
|
| 88 |
st.error(f"❌ Error with {uploaded_file.name}: {e}")
|