Spaces:
Sleeping
Sleeping
File size: 5,179 Bytes
8cbff8c 83fc17f 8cbff8c 83fc17f 8cbff8c 83fc17f 8cbff8c 83fc17f 8cbff8c 83fc17f 8cbff8c 83fc17f 8cbff8c 83fc17f 8cbff8c 83fc17f 8cbff8c 83fc17f 8cbff8c 83fc17f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | import streamlit as st
from PIL import Image, ImageEnhance, ImageOps
import random
import io
# --- Helper Functions for Image Transformations ---
def translate_image(image, x_offset, y_offset):
"""Translate the image by x and y offsets."""
return image.transform(
image.size,
Image.AFFINE,
(1, 0, x_offset, 0, 1, y_offset),
resample=Image.BICUBIC
)
def rotate_image(image, angle):
"""Rotate the image by a given angle."""
return image.rotate(angle, expand=True)
def scale_image(image, scale_factor):
"""Scale the image by a given factor."""
new_size = (int(image.width * scale_factor), int(image.height * scale_factor))
return image.resize(new_size)
def crop_image(image, left, upper, right, lower):
"""Crop the image."""
return image.crop((left, upper, right, lower))
def flip_image(image, flip_type):
"""Flip the image horizontally or vertically."""
if flip_type == 'Horizontal':
return ImageOps.mirror(image)
elif flip_type == 'Vertical':
return ImageOps.flip(image)
return image
# --- Main App Function ---
def main():
st.set_page_config(
page_title="Interactive Image Augmentation Tool",
layout="wide"
)
# --- Styling ---
st.markdown(
"""
<style>
.stTitle {
color: #3E2723;
text-align: center;
font-family: 'Arial', sans-serif;
}
.stButton>button {
background-color: #C19A6B;
color: white;
border-radius: 8px;
}
.stDownloadButton>button {
background-color: #8B5E3C;
color: white;
border-radius: 8px;
}
</style>
""",
unsafe_allow_html=True
)
# --- Title and Introduction ---
st.title("πΌοΈ Interactive Image Augmentation Tool")
st.write("""
Welcome to the **Interactive Image Augmentation Tool**! This app allows you to transform and enhance your images
using various augmentation techniques like **Translation**, **Rotation**, **Scaling**, **Cropping**, and **Flipping**.
""")
st.write("""
### π Instructions for Use:
- Upload an image using the uploader below.
- Select a transformation technique from the dropdown menu.
- Adjust the parameters using sliders and dropdowns.
- Preview the transformed image.
- Download the transformed image if you're satisfied.
""")
st.write("""
### π Who Can Use This App?
- **Data Scientists:** Generate augmented datasets for image classification models.
- **Graphic Designers:** Experiment with creative visual effects.
- **Researchers:** Prepare data for analysis and experiments.
- **Students:** Learn image processing techniques interactively.
""")
# --- File Uploader ---
uploaded_file = st.file_uploader("π€ Upload an Image", type=['jpg', 'jpeg', 'png'])
if uploaded_file:
image = Image.open(uploaded_file)
st.image(image, caption="Original Image", use_column_width=True)
st.subheader("π Apply Transformations")
transformation = st.selectbox(
"Choose a Transformation",
["Translate", "Rotate", "Scale", "Crop", "Flip"]
)
if transformation == "Translate":
x_offset = st.slider("X Offset", -100, 100, 0)
y_offset = st.slider("Y Offset", -100, 100, 0)
transformed_image = translate_image(image, x_offset, y_offset)
elif transformation == "Rotate":
angle = st.slider("Rotation Angle", 0, 360, 0)
transformed_image = rotate_image(image, angle)
elif transformation == "Scale":
scale = st.slider("Scale Factor", 0.5, 2.0, 1.0)
transformed_image = scale_image(image, scale)
elif transformation == "Crop":
left = st.slider("Left", 0, image.width, 0)
upper = st.slider("Upper", 0, image.height, 0)
right = st.slider("Right", left, image.width, image.width)
lower = st.slider("Lower", upper, image.height, image.height)
transformed_image = crop_image(image, left, upper, right, lower)
elif transformation == "Flip":
flip_type = st.selectbox("Flip Type", ["None", "Horizontal", "Vertical"])
transformed_image = flip_image(image, flip_type)
else:
st.warning("Select a valid transformation!")
return
st.image(transformed_image, caption="Transformed Image", use_column_width=True)
# --- Download Image ---
img_buffer = io.BytesIO()
transformed_image.save(img_buffer, format="PNG")
st.download_button(
label="π₯ Download Transformed Image",
data=img_buffer.getvalue(),
file_name="transformed_image.png",
mime="image/png"
)
st.markdown("---")
st.write("π― **Experiment with transformations and download your enhanced images today!**")
if __name__ == "__main__":
main()
|