Spaces:
Sleeping
Sleeping
Prathamesh1420
commited on
Commit
•
210b714
1
Parent(s):
4eb73d8
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image, ImageOps
|
3 |
import io
|
|
|
4 |
|
5 |
# Set Streamlit page configuration
|
6 |
st.set_page_config(page_title="Image Transformer", page_icon="🖼️", layout="wide")
|
@@ -33,7 +34,7 @@ if uploaded_file is not None:
|
|
33 |
with col1:
|
34 |
st.image(image, caption="Original Image", use_column_width=False, width=250)
|
35 |
|
36 |
-
# Variables to store slider values
|
37 |
new_width = None
|
38 |
new_height = None
|
39 |
crop_values = None
|
@@ -41,22 +42,45 @@ if uploaded_file is not None:
|
|
41 |
pixel_size = None
|
42 |
quality = None
|
43 |
|
44 |
-
# Display sliders based on operation, but only apply when the button is pressed
|
45 |
if operation == "Resize":
|
46 |
-
|
47 |
-
|
|
|
48 |
|
49 |
elif operation == "Crop":
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
elif operation == "Rotate":
|
53 |
-
angle = st.
|
54 |
|
55 |
elif operation == "Pixelate":
|
56 |
-
pixel_size = st.
|
57 |
|
58 |
elif operation == "Compress":
|
59 |
-
quality = st.
|
60 |
|
61 |
# Apply button with dynamic label based on the operation
|
62 |
apply_button_label = f"Apply {operation}"
|
@@ -76,7 +100,7 @@ if uploaded_file is not None:
|
|
76 |
|
77 |
# Resize Operation
|
78 |
elif operation == "Resize" and new_width and new_height:
|
79 |
-
resized_image = image.resize((new_width, new_height))
|
80 |
st.image(resized_image, caption=" ", use_column_width=False, width=250)
|
81 |
|
82 |
# Download button for resized image
|
@@ -96,7 +120,7 @@ if uploaded_file is not None:
|
|
96 |
|
97 |
# Rotate Operation
|
98 |
elif operation == "Rotate" and angle is not None:
|
99 |
-
rotated_image = image.rotate(angle)
|
100 |
st.image(rotated_image, caption=" ", use_column_width=False, width=250)
|
101 |
|
102 |
# Download button for rotated image
|
@@ -127,10 +151,10 @@ if uploaded_file is not None:
|
|
127 |
|
128 |
# Compress Operation
|
129 |
elif operation == "Compress" and quality:
|
130 |
-
|
131 |
-
|
|
|
|
|
132 |
|
133 |
# Download button for compressed image
|
134 |
-
|
135 |
-
compressed_image.save(buffered, format="PNG")
|
136 |
-
st.download_button("Download Image", data=buffered.getvalue(), file_name="compressed_image.png", mime="image/png")
|
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image, ImageOps
|
3 |
import io
|
4 |
+
from streamlit_drawable_canvas import st_canvas
|
5 |
|
6 |
# Set Streamlit page configuration
|
7 |
st.set_page_config(page_title="Image Transformer", page_icon="🖼️", layout="wide")
|
|
|
34 |
with col1:
|
35 |
st.image(image, caption="Original Image", use_column_width=False, width=250)
|
36 |
|
37 |
+
# Variables to store slider values or manual inputs
|
38 |
new_width = None
|
39 |
new_height = None
|
40 |
crop_values = None
|
|
|
42 |
pixel_size = None
|
43 |
quality = None
|
44 |
|
45 |
+
# Display sliders/input fields based on operation, but only apply when the button is pressed
|
46 |
if operation == "Resize":
|
47 |
+
st.write("Resize Image:")
|
48 |
+
new_width = st.number_input("Width (in px)", min_value=50, max_value=2000, value=image.width)
|
49 |
+
new_height = st.number_input("Height (in px)", min_value=50, max_value=2000, value=image.height)
|
50 |
|
51 |
elif operation == "Crop":
|
52 |
+
st.write("Crop Image:")
|
53 |
+
st.write("Draw a rectangle on the image to crop it.")
|
54 |
+
canvas_result = st_canvas(
|
55 |
+
stroke_width=1,
|
56 |
+
stroke_color="#FF0000",
|
57 |
+
background_image=image,
|
58 |
+
update_streamlit=True,
|
59 |
+
height=image.height,
|
60 |
+
width=image.width,
|
61 |
+
drawing_mode="rect",
|
62 |
+
key="crop_canvas"
|
63 |
+
)
|
64 |
+
if canvas_result.json_data is not None:
|
65 |
+
try:
|
66 |
+
# Get the rectangle from the canvas
|
67 |
+
rect = canvas_result.json_data["objects"][0]
|
68 |
+
left = int(rect["left"])
|
69 |
+
top = int(rect["top"])
|
70 |
+
width = int(rect["width"])
|
71 |
+
height = int(rect["height"])
|
72 |
+
crop_values = (left, top, left + width, top + height)
|
73 |
+
except IndexError:
|
74 |
+
st.write("No rectangle drawn yet.")
|
75 |
|
76 |
elif operation == "Rotate":
|
77 |
+
angle = st.number_input("Rotate Image clockwise (in degrees)", min_value=0, max_value=360, value=0)
|
78 |
|
79 |
elif operation == "Pixelate":
|
80 |
+
pixel_size = st.number_input("Pixelate Size (larger number = more pixelation)", min_value=1, max_value=50, value=10)
|
81 |
|
82 |
elif operation == "Compress":
|
83 |
+
quality = st.number_input("Image Quality (Compression)", min_value=10, max_value=100, value=80)
|
84 |
|
85 |
# Apply button with dynamic label based on the operation
|
86 |
apply_button_label = f"Apply {operation}"
|
|
|
100 |
|
101 |
# Resize Operation
|
102 |
elif operation == "Resize" and new_width and new_height:
|
103 |
+
resized_image = image.resize((new_width, new_height), Image.LANCZOS)
|
104 |
st.image(resized_image, caption=" ", use_column_width=False, width=250)
|
105 |
|
106 |
# Download button for resized image
|
|
|
120 |
|
121 |
# Rotate Operation
|
122 |
elif operation == "Rotate" and angle is not None:
|
123 |
+
rotated_image = image.rotate(-angle) # Negative to rotate clockwise
|
124 |
st.image(rotated_image, caption=" ", use_column_width=False, width=250)
|
125 |
|
126 |
# Download button for rotated image
|
|
|
151 |
|
152 |
# Compress Operation
|
153 |
elif operation == "Compress" and quality:
|
154 |
+
# Save the image with the specified quality
|
155 |
+
buffered = io.BytesIO()
|
156 |
+
image.save(buffered, format="JPEG", quality=quality)
|
157 |
+
st.image(image, caption=" ", use_column_width=False, width=250)
|
158 |
|
159 |
# Download button for compressed image
|
160 |
+
st.download_button("Download Image", data=buffered.getvalue(), file_name="compressed_image.jpeg", mime="image/jpeg")
|
|
|
|