File size: 3,111 Bytes
20ce6c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, ImageFilter
import numpy as np
import cv2
import io

# Function to adjust brightness, contrast, saturation
def adjust_image(img, brightness=1.0, contrast=1.0, saturation=1.0):
    enhancer = ImageEnhance.Brightness(img)
    img = enhancer.enhance(brightness)
    
    enhancer = ImageEnhance.Contrast(img)
    img = enhancer.enhance(contrast)
    
    enhancer = ImageEnhance.Color(img)
    img = enhancer.enhance(saturation)
    
    return img

# Function to apply grayscale filter
def apply_grayscale(img):
    return img.convert("L")

# Function to apply blur effect
def apply_blur(img):
    return img.filter(ImageFilter.GaussianBlur(radius=5))

# Function to crop image
def crop_image(img, left, upper, right, lower):
    return img.crop((left, upper, right, lower))

# Function to cut a piece of the image
def cut_piece(img, x, y, width, height):
    img_np = np.array(img)
    cut_img = img_np[y:y+height, x:x+width]
    return Image.fromarray(cut_img)

# Streamlit App UI
st.title("Image Editing App")

uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])

if uploaded_file is not None:
    # Open the uploaded image
    img = Image.open(uploaded_file)
    
    st.image(img, caption="Uploaded Image", use_column_width=True)
    
    # Adjust brightness, contrast, and saturation
    st.sidebar.header("Adjust Image")
    brightness = st.sidebar.slider("Brightness", 0.0, 2.0, 1.0)
    contrast = st.sidebar.slider("Contrast", 0.0, 2.0, 1.0)
    saturation = st.sidebar.slider("Saturation", 0.0, 2.0, 1.0)
    
    img = adjust_image(img, brightness, contrast, saturation)
    
    # Filters and effects
    filter_option = st.sidebar.selectbox(
        "Select a Filter",
        ["None", "Grayscale", "Blur"]
    )
    
    if filter_option == "Grayscale":
        img = apply_grayscale(img)
    elif filter_option == "Blur":
        img = apply_blur(img)
    
    # Crop and cut image functionality
    crop_option = st.sidebar.checkbox("Crop Image")
    if crop_option:
        left = st.sidebar.slider("Left", 0, img.width, 0)
        upper = st.sidebar.slider("Upper", 0, img.height, 0)
        right = st.sidebar.slider("Right", left, img.width, img.width)
        lower = st.sidebar.slider("Lower", upper, img.height, img.height)
        img = crop_image(img, left, upper, right, lower)
    
    cut_option = st.sidebar.checkbox("Cut a Piece of Image")
    if cut_option:
        x = st.sidebar.slider("X position", 0, img.width, 0)
        y = st.sidebar.slider("Y position", 0, img.height, 0)
        width = st.sidebar.slider("Width", 1, img.width - x, 100)
        height = st.sidebar.slider("Height", 1, img.height - y, 100)
        img = cut_piece(img, x, y, width, height)
    
    # Display edited image
    st.image(img, caption="Edited Image", use_column_width=True)
    
    # Option to download edited image
    buffered = io.BytesIO()
    img.save(buffered, format="PNG")
    st.download_button(label="Download Image", data=buffered.getvalue(), file_name="edited_image.png", mime="image/png")