File size: 7,123 Bytes
c707ea9
 
4bd0973
210b714
8ccb181
c707ea9
 
 
4fef606
 
4bd3655
4fef606
 
 
 
 
c707ea9
4fef606
 
 
 
 
 
 
 
4bd3655
 
eaa9a88
 
 
 
4bd3655
eaa9a88
4bd3655
 
 
210b714
4eb73d8
 
 
 
 
 
 
210b714
4eb73d8
210b714
 
 
4eb73d8
 
210b714
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4eb73d8
 
210b714
4eb73d8
 
210b714
4eb73d8
 
210b714
4eb73d8
 
 
 
 
 
eaa9a88
 
 
4bd3655
eaa9a88
 
4bd3655
 
 
 
 
eaa9a88
4eb73d8
210b714
eaa9a88
4bd3655
 
 
 
 
 
eaa9a88
4eb73d8
4bd3655
eaa9a88
4bd3655
 
 
 
 
 
eaa9a88
4eb73d8
210b714
eaa9a88
4bd3655
 
 
 
 
 
eaa9a88
 
4bd3655
eaa9a88
4bd3655
 
 
 
 
 
eaa9a88
4eb73d8
4bd3655
 
eaa9a88
4bd3655
 
 
 
 
 
eaa9a88
4eb73d8
210b714
 
 
 
4bd3655
 
210b714
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
150
151
152
153
154
155
156
157
158
159
160
161
import streamlit as st
from PIL import Image, ImageOps
import io
from streamlit_drawable_canvas import st_canvas

# Set Streamlit page configuration
st.set_page_config(page_title="Image Transformer", page_icon="🖼️", layout="wide")

# Sidebar for navigation and options
st.sidebar.title("Transformation Options")
operation = st.sidebar.radio(
    "Choose an operation to perform:",
    ("Mirror", "Resize", "Crop", "Rotate", "Black/White", "Pixelate", "Compress")
)

# Main title for the app
st.title("Image Manipulation with Generative AI")
st.write("Upload an image and apply transformations based on your selection")

# Upload image
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])

if uploaded_file is not None:
    # Load the image
    image = Image.open(uploaded_file)

    # Create two columns for displaying images
    col1, col3 = st.columns([1, 1])
    
    # Create a middle column for the "Apply" button
    col2 = st.empty()  # This will hold the button

    # Show the original image (smaller size) in the first column
    with col1:
        st.image(image, caption="Original Image", use_column_width=False, width=250)

    # Variables to store slider values or manual inputs
    new_width = None
    new_height = None
    crop_values = None
    angle = None
    pixel_size = None
    quality = None

    # Display sliders/input fields based on operation, but only apply when the button is pressed
    if operation == "Resize":
        st.write("Resize Image:")
        new_width = st.number_input("Width (in px)", min_value=50, max_value=2000, value=image.width)
        new_height = st.number_input("Height (in px)", min_value=50, max_value=2000, value=image.height)

    elif operation == "Crop":
        st.write("Crop Image:")
        st.write("Draw a rectangle on the image to crop it.")
        canvas_result = st_canvas(
            stroke_width=1,
            stroke_color="#FF0000",
            background_image=image,
            update_streamlit=True,
            height=image.height,
            width=image.width,
            drawing_mode="rect",
            key="crop_canvas"
        )
        if canvas_result.json_data is not None:
            try:
                # Get the rectangle from the canvas
                rect = canvas_result.json_data["objects"][0]
                left = int(rect["left"])
                top = int(rect["top"])
                width = int(rect["width"])
                height = int(rect["height"])
                crop_values = (left, top, left + width, top + height)
            except IndexError:
                st.write("No rectangle drawn yet.")

    elif operation == "Rotate":
        angle = st.number_input("Rotate Image clockwise (in degrees)", min_value=0, max_value=360, value=0)

    elif operation == "Pixelate":
        pixel_size = st.number_input("Pixelate Size (larger number = more pixelation)", min_value=1, max_value=50, value=10)

    elif operation == "Compress":
        quality = st.number_input("Image Quality (Compression)", min_value=10, max_value=100, value=80)

    # Apply button with dynamic label based on the operation
    apply_button_label = f"Apply {operation}"
    
    if col2.button(apply_button_label):
        # Only generate the output when the "Apply" button is clicked
        with col3:
            # Mirror Operation
            if operation == "Mirror":
                mirrored_image = ImageOps.mirror(image)
                st.image(mirrored_image, caption=" ", use_column_width=False, width=250)

                # Download button for mirrored image
                buffered = io.BytesIO()
                mirrored_image.save(buffered, format="PNG")
                st.download_button("Download Image", data=buffered.getvalue(), file_name="mirrored_image.png", mime="image/png")

            # Resize Operation
            elif operation == "Resize" and new_width and new_height:
                resized_image = image.resize((new_width, new_height), Image.LANCZOS)
                st.image(resized_image, caption=" ", use_column_width=False, width=250)

                # Download button for resized image
                buffered = io.BytesIO()
                resized_image.save(buffered, format="PNG")
                st.download_button("Download Image", data=buffered.getvalue(), file_name="resized_image.png", mime="image/png")

            # Crop Operation
            elif operation == "Crop" and crop_values:
                cropped_image = image.crop(crop_values)
                st.image(cropped_image, caption=" ", use_column_width=False, width=250)

                # Download button for cropped image
                buffered = io.BytesIO()
                cropped_image.save(buffered, format="PNG")
                st.download_button("Download Image", data=buffered.getvalue(), file_name="cropped_image.png", mime="image/png")

            # Rotate Operation
            elif operation == "Rotate" and angle is not None:
                rotated_image = image.rotate(-angle)  # Negative to rotate clockwise
                st.image(rotated_image, caption=" ", use_column_width=False, width=250)

                # Download button for rotated image
                buffered = io.BytesIO()
                rotated_image.save(buffered, format="PNG")
                st.download_button("Download Image", data=buffered.getvalue(), file_name="rotated_image.png", mime="image/png")

            # Black/White Operation
            elif operation == "Black/White":
                bw_image = image.convert("L")
                st.image(bw_image, caption=" ", use_column_width=False, width=250)

                # Download button for black-and-white image
                buffered = io.BytesIO()
                bw_image.save(buffered, format="PNG")
                st.download_button("Download Image", data=buffered.getvalue(), file_name="bw_image.png", mime="image/png")

            # Pixelate Operation
            elif operation == "Pixelate" and pixel_size:
                small = image.resize((image.width // pixel_size, image.height // pixel_size), Image.NEAREST)
                pixelated_image = small.resize((image.width, image.height), Image.NEAREST)
                st.image(pixelated_image, caption=" ", use_column_width=False, width=250)

                # Download button for pixelated image
                buffered = io.BytesIO()
                pixelated_image.save(buffered, format="PNG")
                st.download_button("Download Image", data=buffered.getvalue(), file_name="pixelated_image.png", mime="image/png")

            # Compress Operation
            elif operation == "Compress" and quality:
                # Save the image with the specified quality
                buffered = io.BytesIO()
                image.save(buffered, format="JPEG", quality=quality)
                st.image(image, caption=" ", use_column_width=False, width=250)

                # Download button for compressed image
                st.download_button("Download Image", data=buffered.getvalue(), file_name="compressed_image.jpeg", mime="image/jpeg")