Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ st.set_page_config(page_title="Image Transformer", page_icon="🖼️", layout="
|
|
| 7 |
|
| 8 |
# Sidebar for navigation and options
|
| 9 |
st.sidebar.title("Transformation Options")
|
| 10 |
-
operation = st.sidebar.
|
| 11 |
"Choose an operation to perform:",
|
| 12 |
("Mirror", "Resize", "Crop", "Rotate", "Black/White", "Pixelate", "Compress")
|
| 13 |
)
|
|
@@ -22,91 +22,103 @@ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg
|
|
| 22 |
if uploaded_file is not None:
|
| 23 |
# Load the image
|
| 24 |
image = Image.open(uploaded_file)
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
# Show the original image (smaller size)
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
st.
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
st.
|
| 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 |
-
st.
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
st.
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Sidebar for navigation and options
|
| 9 |
st.sidebar.title("Transformation Options")
|
| 10 |
+
operation = st.sidebar.radio(
|
| 11 |
"Choose an operation to perform:",
|
| 12 |
("Mirror", "Resize", "Crop", "Rotate", "Black/White", "Pixelate", "Compress")
|
| 13 |
)
|
|
|
|
| 22 |
if uploaded_file is not None:
|
| 23 |
# Load the image
|
| 24 |
image = Image.open(uploaded_file)
|
| 25 |
+
|
| 26 |
+
# Create two columns for displaying images
|
| 27 |
+
col1, col2 = st.columns(2)
|
| 28 |
+
|
| 29 |
# Show the original image (smaller size)
|
| 30 |
+
with col1:
|
| 31 |
+
st.image(image, caption="Original Image", use_column_width=False, width=250)
|
| 32 |
+
|
| 33 |
+
# Apply transformations and show the result in the right column
|
| 34 |
+
with col2:
|
| 35 |
+
# Mirror Operation
|
| 36 |
+
if operation == "Mirror":
|
| 37 |
+
st.header("Mirrored Image")
|
| 38 |
+
if st.button("Apply Mirror"):
|
| 39 |
+
mirrored_image = ImageOps.mirror(image)
|
| 40 |
+
st.image(mirrored_image, caption="Mirrored Image", use_column_width=False, width=250)
|
| 41 |
+
|
| 42 |
+
# Download button for mirrored image
|
| 43 |
+
buffered = io.BytesIO()
|
| 44 |
+
mirrored_image.save(buffered, format="PNG")
|
| 45 |
+
st.download_button("Download Image", data=buffered.getvalue(), file_name="mirrored_image.png", mime="image/png")
|
| 46 |
+
|
| 47 |
+
# Resize Operation
|
| 48 |
+
elif operation == "Resize":
|
| 49 |
+
st.header("Resized Image")
|
| 50 |
+
new_width = st.slider("Resize Image - Width", 50, 500, 100)
|
| 51 |
+
new_height = st.slider("Resize Image - Height", 50, 500, 100)
|
| 52 |
+
if st.button("Apply Resize"):
|
| 53 |
+
resized_image = image.resize((new_width, new_height))
|
| 54 |
+
st.image(resized_image, caption="Resized Image", use_column_width=False, width=250)
|
| 55 |
+
|
| 56 |
+
# Download button for resized image
|
| 57 |
+
buffered = io.BytesIO()
|
| 58 |
+
resized_image.save(buffered, format="PNG")
|
| 59 |
+
st.download_button("Download Image", data=buffered.getvalue(), file_name="resized_image.png", mime="image/png")
|
| 60 |
+
|
| 61 |
+
# Crop Operation
|
| 62 |
+
elif operation == "Crop":
|
| 63 |
+
st.header("Cropped Image")
|
| 64 |
+
crop_values = st.slider("Crop Image - (left, upper, right, lower)", 0, 100, (10, 10, 90, 90))
|
| 65 |
+
if st.button("Apply Crop"):
|
| 66 |
+
cropped_image = image.crop(crop_values)
|
| 67 |
+
st.image(cropped_image, caption="Cropped Image", use_column_width=False, width=250)
|
| 68 |
+
|
| 69 |
+
# Download button for cropped image
|
| 70 |
+
buffered = io.BytesIO()
|
| 71 |
+
cropped_image.save(buffered, format="PNG")
|
| 72 |
+
st.download_button("Download Image", data=buffered.getvalue(), file_name="cropped_image.png", mime="image/png")
|
| 73 |
+
|
| 74 |
+
# Rotate Operation
|
| 75 |
+
elif operation == "Rotate":
|
| 76 |
+
st.header("Rotated Image")
|
| 77 |
+
angle = st.slider("Rotate Image", 0, 360, 0)
|
| 78 |
+
if st.button("Apply Rotate"):
|
| 79 |
+
rotated_image = image.rotate(angle)
|
| 80 |
+
st.image(rotated_image, caption="Rotated Image", use_column_width=False, width=250)
|
| 81 |
+
|
| 82 |
+
# Download button for rotated image
|
| 83 |
+
buffered = io.BytesIO()
|
| 84 |
+
rotated_image.save(buffered, format="PNG")
|
| 85 |
+
st.download_button("Download Image", data=buffered.getvalue(), file_name="rotated_image.png", mime="image/png")
|
| 86 |
+
|
| 87 |
+
# Black/White Operation
|
| 88 |
+
elif operation == "Black/White":
|
| 89 |
+
st.header("Black & White Image")
|
| 90 |
+
if st.button("Apply Black/White"):
|
| 91 |
+
bw_image = image.convert("L")
|
| 92 |
+
st.image(bw_image, caption="Black & White Image", use_column_width=False, width=250)
|
| 93 |
+
|
| 94 |
+
# Download button for black-and-white image
|
| 95 |
+
buffered = io.BytesIO()
|
| 96 |
+
bw_image.save(buffered, format="PNG")
|
| 97 |
+
st.download_button("Download Image", data=buffered.getvalue(), file_name="bw_image.png", mime="image/png")
|
| 98 |
+
|
| 99 |
+
# Pixelate Operation
|
| 100 |
+
elif operation == "Pixelate":
|
| 101 |
+
st.header("Pixelated Image")
|
| 102 |
+
pixel_size = st.slider("Pixelate Image", 1, 20, 10)
|
| 103 |
+
if st.button("Apply Pixelate"):
|
| 104 |
+
small = image.resize((image.width // pixel_size, image.height // pixel_size), Image.NEAREST)
|
| 105 |
+
pixelated_image = small.resize((image.width, image.height), Image.NEAREST)
|
| 106 |
+
st.image(pixelated_image, caption="Pixelated Image", use_column_width=False, width=250)
|
| 107 |
+
|
| 108 |
+
# Download button for pixelated image
|
| 109 |
+
buffered = io.BytesIO()
|
| 110 |
+
pixelated_image.save(buffered, format="PNG")
|
| 111 |
+
st.download_button("Download Image", data=buffered.getvalue(), file_name="pixelated_image.png", mime="image/png")
|
| 112 |
+
|
| 113 |
+
# Compress Operation
|
| 114 |
+
elif operation == "Compress":
|
| 115 |
+
st.header("Compressed Image")
|
| 116 |
+
quality = st.slider("Compress Image (Quality)", 10, 100, 80)
|
| 117 |
+
if st.button("Apply Compress"):
|
| 118 |
+
compressed_image = image.copy()
|
| 119 |
+
st.image(compressed_image, caption=f"Compressed Image (Quality: {quality})", use_column_width=False, width=250)
|
| 120 |
+
|
| 121 |
+
# Download button for compressed image
|
| 122 |
+
buffered = io.BytesIO()
|
| 123 |
+
compressed_image.save(buffered, format="PNG")
|
| 124 |
+
st.download_button("Download Image", data=buffered.getvalue(), file_name="compressed_image.png", mime="image/png")
|