File size: 5,589 Bytes
d749323
 
b015623
d749323
2db9a3c
d749323
aa90ae8
d749323
b015623
df91e20
aa90ae8
 
d749323
aa90ae8
 
 
 
 
 
 
757cd20
d749323
 
aa90ae8
d749323
 
aa90ae8
d749323
 
aa90ae8
 
d749323
 
 
aa90ae8
757cd20
 
 
 
 
 
aa90ae8
 
 
 
757cd20
 
 
d749323
757cd20
 
 
 
 
d749323
757cd20
 
 
 
b015623
 
757cd20
b015623
 
 
 
 
757cd20
 
 
 
 
 
 
b015623
 
757cd20
 
 
 
 
 
b015623
757cd20
 
 
b015623
757cd20
 
 
b015623
757cd20
 
 
b015623
757cd20
 
 
b015623
757cd20
 
 
 
b015623
757cd20
 
 
 
b015623
757cd20
 
 
b015623
757cd20
 
 
b015623
757cd20
 
 
b015623
757cd20
 
b015623
2db9a3c
 
 
 
 
 
 
 
 
 
 
 
 
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# app.py
import streamlit as st
from PIL import Image, ImageOps, ImageFilter, ImageEnhance
import io
import os

st.set_page_config(page_title="Image Filter App", layout="wide")

st.title("🖼️ Fun Image Filter App")

# 3-column layout
col1, col2, col3 = st.columns([1, 1, 1])  # equal width

with col1:
    st.header("📤 Upload Image")
    st.caption("Max 5MB • Only PNG, JPG")
    uploaded_file = st.file_uploader("Choose an image", type=["png", "jpg", "jpeg"])

image = None
filtered_image = None
filter_option = "None"

if uploaded_file:
    # Size check
    uploaded_file.seek(0, io.SEEK_END)
    file_size = uploaded_file.tell()
    uploaded_file.seek(0)

    if file_size > 5 * 1024 * 1024:
        with col1:
            st.error("❌ File too large! Must be under 5MB.")
    else:
        image = Image.open(uploaded_file)

        with col2:
            st.header("🎛️ Select Filter")
            filter_option = st.selectbox("Choose a filter:", [
                "None", "Grayscale", "Invert Colors", "Sepia", "Blur", "Edge Detection", 
                "Posterize", "Emboss", "Solarize", "Brightness Boost", "Contrast Boost", 
                "Flip Horizontal", "Flip Vertical", "Rotate 90°"
            ])

        with col3:
            st.header("🔍 Preview Output")

            if filter_option == "Grayscale":
                filtered_image = ImageOps.grayscale(image)
                st.image(filtered_image, caption="Grayscale", use_container_width=True)

            elif filter_option == "Invert Colors":
                if image.mode != "RGB":
                    image = image.convert("RGB")
                filtered_image = ImageOps.invert(image)
                st.image(filtered_image, caption="Inverted", use_container_width=True)

            elif filter_option == "Sepia":
                # Sepia filter
                width, height = image.size
                pixels = image.load()  # create the pixel map
                for py in range(height):
                    for px in range(width):
                        r, g, b = image.getpixel((px, py))

                        tr = int(0.393 * r + 0.769 * g + 0.189 * b)
                        tg = int(0.349 * r + 0.686 * g + 0.168 * b)
                        tb = int(0.272 * r + 0.534 * g + 0.131 * b)

                        # Apply filter
                        if tr > 255:
                            tr = 255
                        if tg > 255:
                            tg = 255
                        if tb > 255:
                            tb = 255

                        pixels[px, py] = (tr, tg, tb)
                filtered_image = image
                st.image(filtered_image, caption="Sepia", use_container_width=True)

            elif filter_option == "Blur":
                filtered_image = image.filter(ImageFilter.GaussianBlur(radius=5))
                st.image(filtered_image, caption="Blurred", use_container_width=True)

            elif filter_option == "Edge Detection":
                filtered_image = image.filter(ImageFilter.FIND_EDGES)
                st.image(filtered_image, caption="Edge Detection", use_container_width=True)

            elif filter_option == "Posterize":
                filtered_image = ImageOps.posterize(image, 4)
                st.image(filtered_image, caption="Posterized", use_container_width=True)

            elif filter_option == "Emboss":
                filtered_image = image.filter(ImageFilter.EMBOSS)
                st.image(filtered_image, caption="Emboss", use_container_width=True)

            elif filter_option == "Solarize":
                filtered_image = ImageOps.solarize(image, threshold=128)
                st.image(filtered_image, caption="Solarized", use_container_width=True)

            elif filter_option == "Brightness Boost":
                enhancer = ImageEnhance.Brightness(image)
                filtered_image = enhancer.enhance(2)  # increase brightness
                st.image(filtered_image, caption="Brightness Boosted", use_container_width=True)

            elif filter_option == "Contrast Boost":
                enhancer = ImageEnhance.Contrast(image)
                filtered_image = enhancer.enhance(2)  # increase contrast
                st.image(filtered_image, caption="Contrast Boosted", use_container_width=True)

            elif filter_option == "Flip Horizontal":
                filtered_image = image.transpose(Image.FLIP_LEFT_RIGHT)
                st.image(filtered_image, caption="Flipped Horizontal", use_container_width=True)

            elif filter_option == "Flip Vertical":
                filtered_image = image.transpose(Image.FLIP_TOP_BOTTOM)
                st.image(filtered_image, caption="Flipped Vertical", use_container_width=True)

            elif filter_option == "Rotate 90°":
                filtered_image = image.rotate(90)
                st.image(filtered_image, caption="Rotated 90°", use_container_width=True)

            else:
                st.info("Select a filter to see output.")

        with col1:
            if filtered_image:
                # Prepend "DEEZ_" to the original file name
                file_name = uploaded_file.name
                file_name_with_prefix = "DEEZ_" + file_name

                # Allow downloading the filtered image with the modified name
                st.download_button(
                    label="Download Filtered Image",
                    data=filtered_image.tobytes(),
                    file_name=file_name_with_prefix,
                    mime="image/png"
                )