File size: 5,088 Bytes
a2a2cd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from PIL import Image, ImageOps
import numpy as np
import io

# Background color for the app
st.markdown(
    """
    <style>
    .stApp {
        background-color: #1E1E1E;
        color: #FFFFFF;
    }
    </style>
    """,
    unsafe_allow_html=True,
)

st.title("🔧 Image Data Augmentation Tool")
st.write("Enhance your images with various augmentation techniques.")

# Image uploader
uploaded_image = st.file_uploader("📤 Upload an Image (jpg, png, jpeg)", type=["jpg", "png", "jpeg"])

if uploaded_image:
    try:
        image = Image.open(uploaded_image)
        st.image(image, caption="Uploaded Image", use_column_width=True)

        st.subheader("🎨 Choose Augmentation Options")

        # Augmentation option
        augmentation_option = st.selectbox(
            "Select an Augmentation Technique:",
            [
                "None",
                "Cropping",
                "Flipping",
                "Rotation",
                "Zooming In",
                "Zooming Out",
                "Translation",
                "Shearing",
                "Grayscale",
                "Invert Colors",
            ],
        )

        augmented_image = image.copy()

        # Cropping
        if augmentation_option == "Cropping":
            col1, col2 = st.columns(2)
            with col1:
                left = st.slider("Left Crop", 0, image.width // 2, 0)
                top = st.slider("Top Crop", 0, image.height // 2, 0)
            with col2:
                right = st.slider("Right Crop", 0, image.width // 2, 0)
                bottom = st.slider("Bottom Crop", 0, image.height // 2, 0)
            augmented_image = image.crop((left, top, image.width - right, image.height - bottom))

        # Flipping
        elif augmentation_option == "Flipping":
            flip_option = st.radio("Flip Type", ["Horizontal", "Vertical"])
            augmented_image = ImageOps.mirror(image) if flip_option == "Horizontal" else ImageOps.flip(image)

        # Rotation
        elif augmentation_option == "Rotation":
            rotation_angle = st.slider("Rotation Angle (°)", -180, 180, 0)
            augmented_image = image.rotate(rotation_angle, expand=True)

        # Zooming In
        elif augmentation_option == "Zooming In":
            scale_factor = st.slider("Zoom Factor (%)", 100, 200, 100) / 100.0
            if scale_factor != 1.0:
                new_size = (int(image.width * scale_factor), int(image.height * scale_factor))
                zoomed_image = image.resize(new_size)
                augmented_image = zoomed_image.crop(((new_size[0] - image.width) // 2, 
                                                     (new_size[1] - image.height) // 2, 
                                                     (new_size[0] + image.width) // 2, 
                                                     (new_size[1] + image.height) // 2))

        # Zooming Out
        elif augmentation_option == "Zooming Out":
            scale_factor = st.slider("Zoom Factor (%)", 50, 100, 100) / 100.0
            new_size = (int(image.width * scale_factor), int(image.height * scale_factor))
            zoomed_image = image.resize(new_size)
            padded_image = Image.new("RGB", (image.width, image.height), (255, 255, 255))
            padded_image.paste(zoomed_image, ((image.width - new_size[0]) // 2, (image.height - new_size[1]) // 2))
            augmented_image = padded_image

        # Translation
        elif augmentation_option == "Translation":
            translation_x = st.slider("Horizontal Shift (px)", -100, 100, 0)
            translation_y = st.slider("Vertical Shift (px)", -100, 100, 0)
            augmented_image = image.transform(
                image.size, Image.AFFINE, (1, 0, translation_x, 0, 1, translation_y), fillcolor=(255, 255, 255)
            )

        # Shearing
        elif augmentation_option == "Shearing":
            shear_angle = st.slider("Shearing Angle (°)", -45, 45, 0)
            shear_matrix = (1, np.tan(np.radians(shear_angle)), 0, 0, 1, 0)
            augmented_image = image.transform(
                (image.width + abs(int(image.height * np.tan(np.radians(shear_angle)))), image.height),
                Image.AFFINE,
                shear_matrix,
                fillcolor=(255, 255, 255),
            )

        # Grayscale
        elif augmentation_option == "Grayscale":
            augmented_image = ImageOps.grayscale(image)

        # Invert Colors
        elif augmentation_option == "Invert Colors":
            augmented_image = ImageOps.invert(image)

        # Display augmented image
        st.subheader("🖼️ Augmented Image Preview")
        st.image(augmented_image, caption=f"{augmentation_option} Applied", use_column_width=True)

        # Download button
        buffer = io.BytesIO()
        augmented_image.save(buffer, format="PNG")
        buffer.seek(0)
        st.download_button("💾 Download Augmented Image", buffer, file_name="augmented_image.png")

    except Exception as e:
        st.error(f"❌ An error occurred: {e}")