Spaces:
Running
Running
import streamlit as st | |
import cv2 | |
import numpy as np | |
from PIL import Image | |
import io | |
def process_image(image, operation, rotation_angle=0, blur_value=5): | |
img_array = np.array(image) | |
img_cv = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR) | |
if operation == "Greyscale": | |
processed_img = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY) | |
elif operation == "Rotate": | |
(h, w) = img_cv.shape[:2] | |
center = (w // 2, h // 2) | |
M = cv2.getRotationMatrix2D(center, rotation_angle, 1.0) | |
processed_img = cv2.warpAffine(img_cv, M, (w, h)) | |
elif operation == "Edge Detection": | |
processed_img = cv2.Canny(img_cv, 100, 200) | |
elif operation == "Blur": | |
processed_img = cv2.GaussianBlur(img_cv, (blur_value*2+1, blur_value*2+1), 0) | |
else: | |
processed_img = img_cv | |
return processed_img | |
def main(): | |
st.title("πΌοΈ Image Processing App π¨") | |
st.markdown("Enhance your images with various effects!") | |
uploaded_image = st.file_uploader("π€ Upload an image", type=["jpg", "png", "jpeg"]) | |
if uploaded_image is not None: | |
image = Image.open(uploaded_image) | |
st.markdown("### βοΈ Editing Options") | |
col3, col4 = st.columns(2) | |
with col3: | |
operation = st.selectbox("π§ Select an operation", ["None", "Greyscale", "Rotate", "Edge Detection", "Blur"]) | |
rotation_angle = 0 | |
blur_value = 5 | |
if operation == "Rotate": | |
with col4: | |
rotation_angle = st.slider("π Rotation Angle", -180, 180, 0) | |
elif operation == "Blur": | |
with col4: | |
blur_value = st.slider("π«οΈ Blur Intensity", 1, 25, 5, step=2) | |
col1, col2 = st.columns(2) | |
with col1: | |
st.image(image, caption="πΌοΈ Original Image", use_container_width=True) | |
if operation != "None": | |
processed_img = process_image(image, operation, rotation_angle, blur_value) | |
with col2: | |
st.image(processed_img, caption="β¨ Processed Image", use_container_width=True, channels="GRAY" if operation == "Greyscale" or operation == "Edge Detection" else "BGR") | |
processed_pil = Image.fromarray(cv2.cvtColor(processed_img, cv2.COLOR_BGR2RGB) if operation != "Greyscale" and operation != "Edge Detection" else processed_img) | |
buf = io.BytesIO() | |
processed_pil.save(buf, format="PNG") | |
byte_im = buf.getvalue() | |
st.markdown("### π₯ Download Your Edited Image") | |
st.download_button(label="πΎ Download", data=byte_im, file_name="processed_image.png", mime="image/png") | |
if __name__ == "__main__": | |
main() | |