File size: 5,128 Bytes
a2a2cd0
2a6dd60
a2a2cd0
 
 
b3d27c9
a2a2cd0
 
 
 
b3d27c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2a2cd0
 
 
 
 
 
2a6dd60
a2a2cd0
2a6dd60
a2a2cd0
 
 
 
2a6dd60
a2a2cd0
2a6dd60
b3d27c9
2a6dd60
 
 
a2a2cd0
2a6dd60
 
 
 
 
a2a2cd0
2a6dd60
e6e493d
a2a2cd0
 
 
 
2a6dd60
e6e493d
 
2a6dd60
e6e493d
2a6dd60
e6e493d
2a6dd60
e6e493d
 
b3d27c9
2a6dd60
b3d27c9
 
 
2a6dd60
b3d27c9
 
2a6dd60
 
 
b3d27c9
 
 
2a6dd60
b3d27c9
 
2a6dd60
b3d27c9
 
 
e6e493d
 
b3d27c9
 
 
2a6dd60
 
b3d27c9
e6e493d
 
b3d27c9
 
2a6dd60
 
b3d27c9
 
 
 
2a6dd60
b3d27c9
2a6dd60
a2a2cd0
 
 
 
b3d27c9
a2a2cd0
 
b3d27c9
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
import streamlit as st
from PIL import Image, ImageOps, ImageEnhance
import numpy as np
import io

# Custom CSS for enhanced styling
st.markdown(
    """
    <style>
    .stApp {
        background-color: #121212;
        color: #E0E0E0;
    }
    .stTitle {
        text-align: center;
        color: #4CAF50;
    }
    .css-1d391kg p, .css-1v0mbdj p {
        color: #B0BEC5;
    }
    .css-1aumxhk .stSlider {
        background-color: #4CAF50;
    }
    .css-2trqyj, .css-16idsys p {
        color: #BB86FC;
    }
    </style>
    """,
    unsafe_allow_html=True,
)

st.title("🌟 Image Augmentation Tool Hands-On Experience", anchor=False)

uploaded_image = st.sidebar.file_uploader("πŸ“€ Upload an Image", type=["jpg", "png", "jpeg"])

if uploaded_image:
    try:
        image = Image.open(uploaded_image)
        st.sidebar.image(image, caption="πŸ“Έ Uploaded Image", use_column_width=True)

        st.sidebar.subheader("✨ Augmentation Options")
        
        augmentation_options = st.sidebar.multiselect(
            "Choose augmentations:",
            ["Cropping", "Flipping", "Rotation", "Zoom In", "Zoom Out", "Translation", "Shearing"]
        )
        
        augmented_image = image.copy()
        
        # Live preview toggle
        live_preview = st.sidebar.checkbox("πŸ”„ Live Preview", value=True)

        for augmentation_option in augmentation_options:
            if augmentation_option == "Cropping":
                left = st.slider("Left Crop", 0, image.width // 2, 0)
                top = st.slider("Top Crop", 0, image.height // 2, 0)
                right = st.slider("Right Crop", 0, image.width // 2, 0)
                bottom = st.slider("Bottom Crop", 0, image.height // 2, 0)
                augmented_image = augmented_image.crop((left, top, image.width - right, image.height - bottom))

            elif augmentation_option == "Flipping":
                flip_option = st.selectbox("πŸ”„ Flip Type", ["Horizontal", "Vertical"], key=f"flip_{augmentation_option}")
                if flip_option == "Horizontal":
                    augmented_image = ImageOps.mirror(augmented_image)
                elif flip_option == "Vertical":
                    augmented_image = ImageOps.flip(augmented_image)

            elif augmentation_option == "Rotation":
                rotation_angle = st.slider("β€Ύ Rotation (Β°)", 0, 360, 0)
                augmented_image = augmented_image.rotate(rotation_angle, expand=True)

            elif augmentation_option == "Zoom In":
                scale_factor = st.slider("πŸ” Zoom Factor (%)", 100, 200, 100) / 100.0
                width, height = augmented_image.size
                new_width = int(width * scale_factor)
                new_height = int(height * scale_factor)
                zoomed_image = augmented_image.resize((new_width, new_height))
                augmented_image = zoomed_image.crop(((new_width - width) // 2, (new_height - height) // 2,
                                                    (new_width + width) // 2, (new_height + height) // 2))

            elif augmentation_option == "Zoom Out":
                scale_factor = st.slider("πŸ”Ž Zoom Out Factor (%)", 50, 100, 100) / 100.0
                width, height = augmented_image.size
                new_width = int(width * scale_factor)
                new_height = int(height * scale_factor)
                zoomed_image = augmented_image.resize((new_width, new_height))
                padded_image = Image.new("RGB", (width, height), (0, 0, 0))
                padded_image.paste(zoomed_image, ((width - new_width) // 2, (height - new_height) // 2))
                augmented_image = padded_image

            elif augmentation_option == "Translation":
                translation_x = st.slider("↔ Horizontal Shift (Pixels)", -100, 100, 0)
                translation_y = st.slider("↕ Vertical Shift (Pixels)", -100, 100, 0)
                translation_matrix = (1, 0, translation_x, 0, 1, translation_y)
                augmented_image = augmented_image.transform(
                    augmented_image.size, Image.AFFINE, translation_matrix, fillcolor=(0, 0, 0)
                )

            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 = augmented_image.transform(
                    (augmented_image.width + abs(int(augmented_image.height * np.tan(np.radians(shear_angle)))), augmented_image.height),
                    Image.AFFINE,
                    shear_matrix,
                    fillcolor=(0, 0, 0),
                )
        
        st.subheader("πŸ–ΌοΈ Augmented Image")
        st.image(augmented_image, caption="Augmentation Applied", use_column_width=True)

        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}")