Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,85 +4,90 @@ import numpy as np
|
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
|
| 7 |
-
st.set_page_config(page_title="Image Transformer
|
| 8 |
-
st.title("
|
| 9 |
|
| 10 |
-
uploaded_file = st.file_uploader("Upload an
|
| 11 |
|
| 12 |
if uploaded_file:
|
| 13 |
try:
|
| 14 |
image = Image.open(uploaded_file).convert("RGB")
|
| 15 |
img_array = np.array(image)
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
with st.expander("π¨ Effects"):
|
| 20 |
-
effect = st.selectbox("
|
| 21 |
|
| 22 |
-
with st.expander("
|
| 23 |
-
|
| 24 |
-
flip = st.radio("Flip", ["None", "Horizontal", "Vertical"])
|
| 25 |
|
| 26 |
-
with st.expander("
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
-
with st.expander("
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
# Apply Effect
|
| 40 |
if effect == "Grayscale":
|
| 41 |
transformed = cv2.cvtColor(transformed, cv2.COLOR_RGB2GRAY)
|
| 42 |
-
elif effect == "Blur":
|
| 43 |
-
transformed = cv2.GaussianBlur(transformed, (
|
| 44 |
-
elif effect == "
|
| 45 |
gray = cv2.cvtColor(transformed, cv2.COLOR_RGB2GRAY)
|
| 46 |
-
transformed = cv2.Canny(gray,
|
| 47 |
elif effect == "HSV":
|
| 48 |
transformed = cv2.cvtColor(transformed, cv2.COLOR_RGB2HSV)
|
| 49 |
elif effect == "LAB":
|
| 50 |
transformed = cv2.cvtColor(transformed, cv2.COLOR_RGB2Lab)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
transformed = cv2.flip(transformed, 1)
|
| 55 |
-
|
| 56 |
transformed = cv2.flip(transformed, 0)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
# Show in tabs
|
| 73 |
-
tab1, tab2 = st.tabs(["πΌοΈ Original", "π¨ Transformed"])
|
| 74 |
-
with tab1:
|
| 75 |
-
st.image(img_array, caption="Original", use_column_width=True)
|
| 76 |
-
with tab2:
|
| 77 |
-
st.image(transformed, caption="Transformed", use_column_width=True, channels="GRAY" if len(transformed.shape) == 2 else "RGB")
|
| 78 |
-
|
| 79 |
-
# Download
|
| 80 |
-
final_img = Image.fromarray(cv2.cvtColor(transformed, cv2.COLOR_GRAY2RGB) if len(transformed.shape) == 2 else transformed)
|
| 81 |
-
buffer = io.BytesIO()
|
| 82 |
-
final_img.save(buffer, format="PNG")
|
| 83 |
-
st.download_button("π₯ Download Result", buffer.getvalue(), "transformed.png", "image/png")
|
| 84 |
|
| 85 |
except Exception as e:
|
| 86 |
-
st.error(f"
|
|
|
|
| 87 |
else:
|
| 88 |
-
st.info("
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
|
| 7 |
+
st.set_page_config(page_title="Image Transformer", layout="centered")
|
| 8 |
+
st.title("πΌοΈ Image Transformer")
|
| 9 |
|
| 10 |
+
uploaded_file = st.file_uploader("π€ Upload an Image", type=["jpg", "jpeg", "png"])
|
| 11 |
|
| 12 |
if uploaded_file:
|
| 13 |
try:
|
| 14 |
image = Image.open(uploaded_file).convert("RGB")
|
| 15 |
img_array = np.array(image)
|
| 16 |
+
original_shape = img_array.shape
|
| 17 |
+
|
| 18 |
+
# Show original in smaller size
|
| 19 |
+
st.image(img_array, caption=f"Original Image | {original_shape[1]}x{original_shape[0]}", width=300)
|
| 20 |
|
| 21 |
+
# Sidebar controls
|
| 22 |
+
with st.sidebar.expander("π¨ Effects"):
|
| 23 |
+
effect = st.selectbox("Choose Effect", ["None", "Grayscale", "Gaussian Blur", "Canny Edge Detection", "HSV", "LAB"])
|
| 24 |
|
| 25 |
+
with st.sidebar.expander("π Rotate"):
|
| 26 |
+
rotate = st.slider("Rotate (degrees)", -180, 180, 0)
|
|
|
|
| 27 |
|
| 28 |
+
with st.sidebar.expander("βοΈ Flip"):
|
| 29 |
+
flip_h = st.checkbox("Flip Horizontally")
|
| 30 |
+
flip_v = st.checkbox("Flip Vertically")
|
| 31 |
|
| 32 |
+
with st.sidebar.expander("π Shear & Translate"):
|
| 33 |
+
shear_x = st.slider("Shear X", -0.5, 0.5, 0.0)
|
| 34 |
+
shear_y = st.slider("Shear Y", -0.5, 0.5, 0.0)
|
| 35 |
+
translate_x = st.slider("Translate X", -100, 100, 0)
|
| 36 |
+
translate_y = st.slider("Translate Y", -100, 100, 0)
|
| 37 |
+
|
| 38 |
+
with st.sidebar.expander("βοΈ Crop"):
|
| 39 |
+
crop = st.checkbox("Enable Cropping")
|
| 40 |
+
if crop:
|
| 41 |
+
max_top = original_shape[0] // 2
|
| 42 |
+
max_side = original_shape[1] // 2
|
| 43 |
+
crop_top = st.slider("Crop Top", 0, max_top, 0)
|
| 44 |
+
crop_bottom = st.slider("Crop Bottom", 0, max_top, 0)
|
| 45 |
+
crop_left = st.slider("Crop Left", 0, max_side, 0)
|
| 46 |
+
crop_right = st.slider("Crop Right", 0, max_side, 0)
|
| 47 |
+
|
| 48 |
+
# Start transformation
|
| 49 |
+
transformed = img_array.copy()
|
| 50 |
|
|
|
|
| 51 |
if effect == "Grayscale":
|
| 52 |
transformed = cv2.cvtColor(transformed, cv2.COLOR_RGB2GRAY)
|
| 53 |
+
elif effect == "Gaussian Blur":
|
| 54 |
+
transformed = cv2.GaussianBlur(transformed, (11, 11), 0)
|
| 55 |
+
elif effect == "Canny Edge Detection":
|
| 56 |
gray = cv2.cvtColor(transformed, cv2.COLOR_RGB2GRAY)
|
| 57 |
+
transformed = cv2.Canny(gray, 100, 200)
|
| 58 |
elif effect == "HSV":
|
| 59 |
transformed = cv2.cvtColor(transformed, cv2.COLOR_RGB2HSV)
|
| 60 |
elif effect == "LAB":
|
| 61 |
transformed = cv2.cvtColor(transformed, cv2.COLOR_RGB2Lab)
|
| 62 |
|
| 63 |
+
rows, cols = transformed.shape[:2]
|
| 64 |
+
M = np.float32([[1, shear_x, translate_x], [shear_y, 1, translate_y]])
|
| 65 |
+
transformed = cv2.warpAffine(transformed, M, (cols, rows))
|
| 66 |
+
|
| 67 |
+
M_rotate = cv2.getRotationMatrix2D((cols / 2, rows / 2), rotate, 1)
|
| 68 |
+
transformed = cv2.warpAffine(transformed, M_rotate, (cols, rows))
|
| 69 |
+
|
| 70 |
+
if flip_h:
|
| 71 |
transformed = cv2.flip(transformed, 1)
|
| 72 |
+
if flip_v:
|
| 73 |
transformed = cv2.flip(transformed, 0)
|
| 74 |
|
| 75 |
+
if crop:
|
| 76 |
+
transformed = transformed[crop_top:rows - crop_bottom, crop_left:cols - crop_right]
|
| 77 |
+
|
| 78 |
+
# Show transformed in smaller size
|
| 79 |
+
st.image(transformed, caption="Transformed Image", width=300, channels="GRAY" if len(transformed.shape) == 2 else "RGB")
|
| 80 |
+
|
| 81 |
+
# Download button
|
| 82 |
+
final_image = Image.fromarray(cv2.cvtColor(transformed, cv2.COLOR_GRAY2RGB) if len(transformed.shape) == 2 else transformed)
|
| 83 |
+
buf = io.BytesIO()
|
| 84 |
+
final_image.save(buf, format="PNG")
|
| 85 |
+
byte_im = buf.getvalue()
|
| 86 |
+
|
| 87 |
+
st.download_button("π₯ Download Transformed Image", byte_im, "transformed.png", "image/png")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
+
st.error(f"β Error: {e}")
|
| 91 |
+
|
| 92 |
else:
|
| 93 |
+
st.info("π Upload an image to get started.")
|