File size: 3,842 Bytes
fb7892e
012f223
fb7892e
 
012f223
 
 
 
 
 
eb969e8
012f223
fb7892e
012f223
 
 
 
 
 
 
 
 
 
 
 
fb7892e
012f223
 
 
 
fb7892e
012f223
 
 
 
 
 
 
 
 
 
 
 
 
 
fb7892e
012f223
 
 
 
 
fb7892e
012f223
 
eb969e8
 
012f223
 
 
 
cf999d6
012f223
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf999d6
012f223
eb969e8
012f223
eb969e8
 
 
012f223
 
 
 
 
 
 
 
 
 
 
eb969e8
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
93
94
95
96
97
98
99
100
101
102
103
104
import streamlit as st
from PIL import Image, ImageEnhance, ImageFilter, ImageOps
import numpy as np

# Function to apply filters
def apply_filter(image, filter_type):
    if filter_type == 'Grayscale':
        return image.convert('L')
    elif filter_type == 'Brightness':
        enhancer = ImageEnhance.Brightness(image)
        return enhancer.enhance(1.5)  # Adjust brightness as per preference
    return image

# Function to adjust brightness, contrast, saturation
def adjust_brightness_contrast_saturation(image, brightness, contrast, saturation):
    enhancer_brightness = ImageEnhance.Brightness(image)
    image = enhancer_brightness.enhance(brightness)
    
    enhancer_contrast = ImageEnhance.Contrast(image)
    image = enhancer_contrast.enhance(contrast)
    
    enhancer_saturation = ImageEnhance.Color(image)
    image = enhancer_saturation.enhance(saturation)
    
    return image

# Function to apply sharpness
def adjust_sharpness(image, sharpness_factor):
    enhancer = ImageEnhance.Sharpness(image)
    return enhancer.enhance(sharpness_factor)

# Function to resize image
def resize_image(image, width, height):
    return image.resize((width, height))

# Function to rotate image
def rotate_image(image, angle):
    return image.rotate(angle)

# Function to flip image
def flip_image(image, direction):
    if direction == 'Horizontal':
        return image.transpose(Image.FLIP_LEFT_RIGHT)
    elif direction == 'Vertical':
        return image.transpose(Image.FLIP_TOP_BOTTOM)

# Main Streamlit interface
def main():
    st.title('Image Editor')

    uploaded_image = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
    
    if uploaded_image is not None:
        image = Image.open(uploaded_image)
        st.image(image, caption="Uploaded Image", use_container_width=True)  # Using container width for better fit

        # Filters and enhancements
        filter_type = st.selectbox("Select Filter", ['None', 'Grayscale', 'Brightness'])
        if filter_type != 'None':
            image = apply_filter(image, filter_type)
            st.image(image, caption="Filtered Image", use_container_width=True)
        
        # Image adjustments
        brightness = st.slider("Brightness", 0.0, 2.0, 1.0)
        contrast = st.slider("Contrast", 0.0, 2.0, 1.0)
        saturation = st.slider("Saturation", 0.0, 2.0, 1.0)
        image = adjust_brightness_contrast_saturation(image, brightness, contrast, saturation)
        
        # Sharpness adjustment
        sharpness_factor = st.slider("Sharpness", 0.0, 2.0, 1.0)
        image = adjust_sharpness(image, sharpness_factor)
        
        # Resize, rotate, and flip options
        width = st.slider("Resize Width", 50, 1000, image.width)
        height = st.slider("Resize Height", 50, 1000, image.height)
        image = resize_image(image, width, height)

        angle = st.slider("Rotation Angle", -180, 180, 0)
        image = rotate_image(image, angle)

        flip_direction = st.selectbox("Flip Direction", ['None', 'Horizontal', 'Vertical'])
        if flip_direction != 'None':
            image = flip_image(image, flip_direction)
        
        st.image(image, caption="Edited Image", use_container_width=True)
        
        # Save image with high quality (lossless or high-quality JPEG)
        if st.button("Download Image"):
            img_path = "edited_image.png"  # Save as PNG for lossless quality
            image.save(img_path, format='PNG')  # Save in PNG format for no quality loss
            
            with open(img_path, "rb") as file:
                st.download_button(
                    label="Download Edited Image",
                    data=file,
                    file_name="edited_image.png",
                    mime="image/png"
                )

if __name__ == "__main__":
    main()