Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +88 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
st.set_page_config(page_title="Image Augmentation Tool", layout="wide")
|
| 8 |
+
|
| 9 |
+
st.markdown(
|
| 10 |
+
"<h1 style='text-align: center; color: #4CAF50;'>📸 Image Augmentation Tool</h1>",
|
| 11 |
+
unsafe_allow_html=True
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
st.markdown("Enhance your images with transformations like rotation, flip, grayscale, crop, and more!")
|
| 15 |
+
|
| 16 |
+
# Sidebar
|
| 17 |
+
with st.sidebar:
|
| 18 |
+
st.header("🛠️ Transformation Options")
|
| 19 |
+
gray = st.checkbox("🖤 Convert to Grayscale")
|
| 20 |
+
rotate = st.slider("🔄 Rotate Image", -180, 180, 0)
|
| 21 |
+
flip_h = st.checkbox("↔️ Flip Horizontally")
|
| 22 |
+
flip_v = st.checkbox("↕️ Flip Vertically")
|
| 23 |
+
shear_x = st.slider("📐 Shear X", -0.5, 0.5, 0.0, step=0.01)
|
| 24 |
+
translate_x = st.slider("➡️ Translate X", -100, 100, 0)
|
| 25 |
+
translate_y = st.slider("⬇️ Translate Y", -100, 100, 0)
|
| 26 |
+
crop = st.checkbox("✂️ Crop Center (50%)")
|
| 27 |
+
|
| 28 |
+
uploaded_file = st.file_uploader("📤 Upload an image", type=["jpg", "jpeg", "png"])
|
| 29 |
+
|
| 30 |
+
if uploaded_file:
|
| 31 |
+
try:
|
| 32 |
+
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 33 |
+
image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
| 34 |
+
orig = image.copy()
|
| 35 |
+
|
| 36 |
+
# Transformations
|
| 37 |
+
if gray:
|
| 38 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 39 |
+
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
|
| 40 |
+
|
| 41 |
+
if rotate != 0:
|
| 42 |
+
h, w = image.shape[:2]
|
| 43 |
+
M = cv2.getRotationMatrix2D((w/2, h/2), rotate, 1)
|
| 44 |
+
image = cv2.warpAffine(image, M, (w, h))
|
| 45 |
+
|
| 46 |
+
if flip_h:
|
| 47 |
+
image = cv2.flip(image, 1)
|
| 48 |
+
if flip_v:
|
| 49 |
+
image = cv2.flip(image, 0)
|
| 50 |
+
|
| 51 |
+
if shear_x != 0.0:
|
| 52 |
+
h, w = image.shape[:2]
|
| 53 |
+
M = np.float32([[1, shear_x, 0], [0, 1, 0]])
|
| 54 |
+
image = cv2.warpAffine(image, M, (w, h))
|
| 55 |
+
|
| 56 |
+
if translate_x != 0 or translate_y != 0:
|
| 57 |
+
M = np.float32([[1, 0, translate_x], [0, 1, translate_y]])
|
| 58 |
+
image = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
|
| 59 |
+
|
| 60 |
+
if crop:
|
| 61 |
+
h, w = image.shape[:2]
|
| 62 |
+
x1, y1 = w // 4, h // 4
|
| 63 |
+
x2, y2 = x1 + w // 2, y1 + h // 2
|
| 64 |
+
image = image[y1:y2, x1:x2]
|
| 65 |
+
|
| 66 |
+
# Display in columns
|
| 67 |
+
col1, col2 = st.columns(2)
|
| 68 |
+
|
| 69 |
+
with col1:
|
| 70 |
+
st.subheader("🖼️ Original Image")
|
| 71 |
+
st.image(cv2.cvtColor(orig, cv2.COLOR_BGR2RGB), use_container_width=True)
|
| 72 |
+
|
| 73 |
+
with col2:
|
| 74 |
+
st.subheader("🎨 Transformed Image")
|
| 75 |
+
st.image(cv2.cvtColor(image, cv2.COLOR_BGR2RGB), use_container_width=True)
|
| 76 |
+
|
| 77 |
+
# Download
|
| 78 |
+
result = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 79 |
+
im_pil = Image.fromarray(result)
|
| 80 |
+
buf = io.BytesIO()
|
| 81 |
+
im_pil.save(buf, format="PNG")
|
| 82 |
+
byte_im = buf.getvalue()
|
| 83 |
+
st.download_button("📥 Download Transformed Image", byte_im, file_name="transformed.png", mime="image/png")
|
| 84 |
+
|
| 85 |
+
except Exception as e:
|
| 86 |
+
st.error(f"❌ Error: {e}")
|
| 87 |
+
else:
|
| 88 |
+
st.info("👆 Please upload an image to begin.")
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
opencv-python
|
| 3 |
+
numpy
|
| 4 |
+
Pillow
|
| 5 |
+
import cv2
|