File size: 3,161 Bytes
30e7ddb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
88
89
90
91
92
import streamlit as st
from PIL import Image, ImageEnhance, ImageOps, ImageFilter
import numpy as np
import cv2
from skimage import filters, transform

# App title
st.title("Image Editor App")

# Sidebar
st.sidebar.title("Features")
uploaded_file = st.sidebar.file_uploader("Upload an Image", type=["png", "jpg", "jpeg"])
if uploaded_file:
    img = Image.open(uploaded_file)
    img_array = np.array(img)

    # Basic Editing
    st.sidebar.subheader("Basic Editing")
    brightness = st.sidebar.slider("Brightness", 0.5, 3.0, 1.0)
    contrast = st.sidebar.slider("Contrast", 0.5, 3.0, 1.0)
    saturation = st.sidebar.slider("Saturation", 0.5, 3.0, 1.0)
    rotation_angle = st.sidebar.slider("Rotate", 0, 360, 0)
    flip_option = st.sidebar.radio("Flip", ["None", "Horizontal", "Vertical"])

    # Advanced Editing
    st.sidebar.subheader("Advanced Editing")
    blur = st.sidebar.slider("Blur", 0, 10, 0)
    edge_detection = st.sidebar.checkbox("Edge Detection")
    crop_left = st.sidebar.slider("Crop Left (%)", 0, 50, 0)
    crop_right = st.sidebar.slider("Crop Right (%)", 0, 50, 0)
    crop_top = st.sidebar.slider("Crop Top (%)", 0, 50, 0)
    crop_bottom = st.sidebar.slider("Crop Bottom (%)", 0, 50, 0)

    # Creative Features
    st.sidebar.subheader("Creative Features")
    text_overlay = st.sidebar.text_input("Text Overlay", "Enter text")
    text_position_x = st.sidebar.slider("Text X Position", 0, img.width, 50)
    text_position_y = st.sidebar.slider("Text Y Position", 0, img.height, 50)

    # Apply transformations
    enhancer = ImageEnhance.Brightness(img)
    img = enhancer.enhance(brightness)
    enhancer = ImageEnhance.Contrast(img)
    img = enhancer.enhance(contrast)
    enhancer = ImageEnhance.Color(img)
    img = enhancer.enhance(saturation)

    if rotation_angle:
        img = img.rotate(rotation_angle)

    if flip_option == "Horizontal":
        img = ImageOps.mirror(img)
    elif flip_option == "Vertical":
        img = ImageOps.flip(img)

    if blur > 0:
        img = img.filter(ImageFilter.GaussianBlur(blur))

    if edge_detection:
        edges = cv2.Canny(np.array(img), 100, 200)
        img = Image.fromarray(edges)

    if any([crop_left, crop_right, crop_top, crop_bottom]):
        width, height = img.size
        img = img.crop((
            width * crop_left // 100,
            height * crop_top // 100,
            width * (100 - crop_right) // 100,
            height * (100 - crop_bottom) // 100
        ))

    # Text Overlay
    if text_overlay:
        img = np.array(img)
        cv2.putText(img, text_overlay, (text_position_x, text_position_y),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv2.LINE_AA)
        img = Image.fromarray(img)

    # Display original and edited images
    st.image(img_array, caption="Original Image", use_column_width=True)
    st.image(img, caption="Edited Image", use_column_width=True)

    # Download option
    st.sidebar.download_button(
        label="Download Edited Image",
        data=img.tobytes(),
        file_name="edited_image.png",
        mime="image/png"
    )
else:
    st.info("Please upload an image to begin.")