Spaces:
Sleeping
Sleeping
Update pages/8_Image_Augumentation.py
Browse files
pages/8_Image_Augumentation.py
CHANGED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
+
import io
|
| 5 |
+
import zipfile
|
| 6 |
+
|
| 7 |
+
# Title of the app
|
| 8 |
+
st.title("Image Transformation App")
|
| 9 |
+
|
| 10 |
+
# Upload image files
|
| 11 |
+
uploaded_files = st.file_uploader("Upload Images", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
|
| 12 |
+
|
| 13 |
+
if uploaded_files:
|
| 14 |
+
images = [Image.open(file) for file in uploaded_files]
|
| 15 |
+
|
| 16 |
+
st.header("Uploaded Images")
|
| 17 |
+
for i, image in enumerate(images):
|
| 18 |
+
st.image(image, caption=f"Image {i+1}", use_column_width=True)
|
| 19 |
+
|
| 20 |
+
# Transformation options
|
| 21 |
+
st.header("Select Transformations")
|
| 22 |
+
options = st.multiselect("Choose transformations:", [
|
| 23 |
+
"Rotation",
|
| 24 |
+
"Scaling",
|
| 25 |
+
"Shearing",
|
| 26 |
+
"Cropping",
|
| 27 |
+
])
|
| 28 |
+
|
| 29 |
+
# Parameters for transformations
|
| 30 |
+
angle = scale_factor = shear_factor = left = top = right = bottom = None
|
| 31 |
+
|
| 32 |
+
if "Rotation" in options:
|
| 33 |
+
angle = st.slider("Rotation Angle (degrees):", 0, 360, 90)
|
| 34 |
+
|
| 35 |
+
if "Scaling" in options:
|
| 36 |
+
scale_factor = st.slider("Scaling Factor:", 0.1, 3.0, 1.0)
|
| 37 |
+
|
| 38 |
+
if "Shearing" in options:
|
| 39 |
+
shear_factor = st.slider("Shearing Factor:", -0.5, 0.5, 0.0)
|
| 40 |
+
|
| 41 |
+
if "Cropping" in options:
|
| 42 |
+
left = st.slider("Left Crop:", 0, min([image.width for image in images]) - 1, 0)
|
| 43 |
+
top = st.slider("Top Crop:", 0, min([image.height for image in images]) - 1, 0)
|
| 44 |
+
right = st.slider("Right Crop:", left + 1, min([image.width for image in images]), min([image.width for image in images]))
|
| 45 |
+
bottom = st.slider("Bottom Crop:", top + 1, min([image.height for image in images]), min([image.height for image in images]))
|
| 46 |
+
|
| 47 |
+
# Perform transformations
|
| 48 |
+
transformed_images = []
|
| 49 |
+
|
| 50 |
+
for image in images:
|
| 51 |
+
transformed_image = image.copy()
|
| 52 |
+
|
| 53 |
+
if "Rotation" in options and angle is not None:
|
| 54 |
+
transformed_image = transformed_image.rotate(angle)
|
| 55 |
+
|
| 56 |
+
if "Scaling" in options and scale_factor is not None:
|
| 57 |
+
new_size = (int(transformed_image.width * scale_factor), int(transformed_image.height * scale_factor))
|
| 58 |
+
transformed_image = transformed_image.resize(new_size)
|
| 59 |
+
|
| 60 |
+
if "Shearing" in options and shear_factor is not None:
|
| 61 |
+
matrix = [
|
| 62 |
+
1, shear_factor, 0,
|
| 63 |
+
0, 1, 0
|
| 64 |
+
]
|
| 65 |
+
transformed_image = transformed_image.transform(transformed_image.size, Image.AFFINE, matrix)
|
| 66 |
+
|
| 67 |
+
if "Cropping" in options and left is not None and top is not None and right is not None and bottom is not None:
|
| 68 |
+
transformed_image = transformed_image.crop((left, top, right, bottom))
|
| 69 |
+
|
| 70 |
+
transformed_images.append(transformed_image)
|
| 71 |
+
|
| 72 |
+
# Display transformed images
|
| 73 |
+
st.header("Transformed Images")
|
| 74 |
+
for i, transformed_image in enumerate(transformed_images):
|
| 75 |
+
st.image(transformed_image, caption=f"Transformed Image {i+1}", use_column_width=True)
|
| 76 |
+
|
| 77 |
+
# Download all transformed images as a zip file
|
| 78 |
+
if transformed_images:
|
| 79 |
+
zip_buffer = io.BytesIO()
|
| 80 |
+
with zipfile.ZipFile(zip_buffer, "w") as zf:
|
| 81 |
+
for i, transformed_image in enumerate(transformed_images):
|
| 82 |
+
img_buffer = io.BytesIO()
|
| 83 |
+
transformed_image.save(img_buffer, format="PNG")
|
| 84 |
+
zf.writestr(f"transformed_image_{i+1}.png", img_buffer.getvalue())
|
| 85 |
+
|
| 86 |
+
zip_buffer.seek(0)
|
| 87 |
+
st.download_button(
|
| 88 |
+
label="Download All Transformed Images",
|
| 89 |
+
data=zip_buffer,
|
| 90 |
+
file_name="Transformed_images.zip",
|
| 91 |
+
mime="application/zip",
|
| 92 |
+
)
|