Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,7 +17,7 @@ with col1:
|
|
| 17 |
|
| 18 |
image = None
|
| 19 |
filtered_image = None
|
| 20 |
-
|
| 21 |
|
| 22 |
if uploaded_file:
|
| 23 |
# Size check
|
|
@@ -30,87 +30,94 @@ if uploaded_file:
|
|
| 30 |
st.error("❌ File too large! Must be under 5MB.")
|
| 31 |
else:
|
| 32 |
image = Image.open(uploaded_file)
|
| 33 |
-
image_copy = image.copy() # Keep a copy for modification
|
| 34 |
|
| 35 |
with col2:
|
| 36 |
-
st.header("🎛️ Select
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
edges = st.checkbox("Edge Detection")
|
| 43 |
-
posterize = st.checkbox("Posterize")
|
| 44 |
-
emboss = st.checkbox("Emboss")
|
| 45 |
-
solarize = st.checkbox("Solarize")
|
| 46 |
-
brightness = st.checkbox("Brightness Boost")
|
| 47 |
-
contrast = st.checkbox("Contrast Boost")
|
| 48 |
-
flip_h = st.checkbox("Flip Horizontal")
|
| 49 |
-
flip_v = st.checkbox("Flip Vertical")
|
| 50 |
-
rotate = st.checkbox("Rotate 90°")
|
| 51 |
|
| 52 |
with col3:
|
| 53 |
st.header("🔍 Preview Output")
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
if sepia:
|
| 68 |
-
width, height = filtered_image.size
|
| 69 |
-
pixels = filtered_image.load()
|
| 70 |
for py in range(height):
|
| 71 |
for px in range(width):
|
| 72 |
-
r, g, b =
|
| 73 |
|
| 74 |
tr = int(0.393 * r + 0.769 * g + 0.189 * b)
|
| 75 |
tg = int(0.349 * r + 0.686 * g + 0.168 * b)
|
| 76 |
tb = int(0.272 * r + 0.534 * g + 0.131 * b)
|
| 77 |
|
| 78 |
-
|
| 79 |
-
if
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
pixels[px, py] = (tr, tg, tb)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
|
| 85 |
-
filtered_image =
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
-
filtered_image =
|
|
|
|
| 89 |
|
| 90 |
-
|
| 91 |
-
filtered_image =
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
filtered_image =
|
|
|
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
| 100 |
-
enhancer = ImageEnhance.
|
| 101 |
-
filtered_image = enhancer.enhance(2)
|
|
|
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
filtered_image =
|
| 106 |
|
| 107 |
-
|
| 108 |
-
filtered_image =
|
|
|
|
| 109 |
|
| 110 |
-
|
| 111 |
-
filtered_image =
|
|
|
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
|
| 116 |
-
st.image(filtered_image, caption="Filtered Image", use_container_width=True)
|
|
|
|
| 17 |
|
| 18 |
image = None
|
| 19 |
filtered_image = None
|
| 20 |
+
filter_option = "None"
|
| 21 |
|
| 22 |
if uploaded_file:
|
| 23 |
# Size check
|
|
|
|
| 30 |
st.error("❌ File too large! Must be under 5MB.")
|
| 31 |
else:
|
| 32 |
image = Image.open(uploaded_file)
|
|
|
|
| 33 |
|
| 34 |
with col2:
|
| 35 |
+
st.header("🎛️ Select Filter")
|
| 36 |
+
filter_option = st.selectbox("Choose a filter:", [
|
| 37 |
+
"None", "Grayscale", "Invert Colors", "Sepia", "Blur", "Edge Detection",
|
| 38 |
+
"Posterize", "Emboss", "Solarize", "Brightness Boost", "Contrast Boost",
|
| 39 |
+
"Flip Horizontal", "Flip Vertical", "Rotate 90°"
|
| 40 |
+
])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
with col3:
|
| 43 |
st.header("🔍 Preview Output")
|
| 44 |
|
| 45 |
+
if filter_option == "Grayscale":
|
| 46 |
+
filtered_image = ImageOps.grayscale(image)
|
| 47 |
+
st.image(filtered_image, caption="Grayscale", use_container_width=True)
|
| 48 |
|
| 49 |
+
elif filter_option == "Invert Colors":
|
| 50 |
+
if image.mode != "RGB":
|
| 51 |
+
image = image.convert("RGB")
|
| 52 |
+
filtered_image = ImageOps.invert(image)
|
| 53 |
+
st.image(filtered_image, caption="Inverted", use_container_width=True)
|
| 54 |
|
| 55 |
+
elif filter_option == "Sepia":
|
| 56 |
+
# Sepia filter
|
| 57 |
+
width, height = image.size
|
| 58 |
+
pixels = image.load() # create the pixel map
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
for py in range(height):
|
| 60 |
for px in range(width):
|
| 61 |
+
r, g, b = image.getpixel((px, py))
|
| 62 |
|
| 63 |
tr = int(0.393 * r + 0.769 * g + 0.189 * b)
|
| 64 |
tg = int(0.349 * r + 0.686 * g + 0.168 * b)
|
| 65 |
tb = int(0.272 * r + 0.534 * g + 0.131 * b)
|
| 66 |
|
| 67 |
+
# Apply filter
|
| 68 |
+
if tr > 255:
|
| 69 |
+
tr = 255
|
| 70 |
+
if tg > 255:
|
| 71 |
+
tg = 255
|
| 72 |
+
if tb > 255:
|
| 73 |
+
tb = 255
|
| 74 |
|
| 75 |
pixels[px, py] = (tr, tg, tb)
|
| 76 |
+
filtered_image = image
|
| 77 |
+
st.image(filtered_image, caption="Sepia", use_container_width=True)
|
| 78 |
+
|
| 79 |
+
elif filter_option == "Blur":
|
| 80 |
+
filtered_image = image.filter(ImageFilter.GaussianBlur(radius=5))
|
| 81 |
+
st.image(filtered_image, caption="Blurred", use_container_width=True)
|
| 82 |
|
| 83 |
+
elif filter_option == "Edge Detection":
|
| 84 |
+
filtered_image = image.filter(ImageFilter.FIND_EDGES)
|
| 85 |
+
st.image(filtered_image, caption="Edge Detection", use_container_width=True)
|
| 86 |
|
| 87 |
+
elif filter_option == "Posterize":
|
| 88 |
+
filtered_image = ImageOps.posterize(image, 4)
|
| 89 |
+
st.image(filtered_image, caption="Posterized", use_container_width=True)
|
| 90 |
|
| 91 |
+
elif filter_option == "Emboss":
|
| 92 |
+
filtered_image = image.filter(ImageFilter.EMBOSS)
|
| 93 |
+
st.image(filtered_image, caption="Emboss", use_container_width=True)
|
| 94 |
|
| 95 |
+
elif filter_option == "Solarize":
|
| 96 |
+
filtered_image = ImageOps.solarize(image, threshold=128)
|
| 97 |
+
st.image(filtered_image, caption="Solarized", use_container_width=True)
|
| 98 |
|
| 99 |
+
elif filter_option == "Brightness Boost":
|
| 100 |
+
enhancer = ImageEnhance.Brightness(image)
|
| 101 |
+
filtered_image = enhancer.enhance(2) # increase brightness
|
| 102 |
+
st.image(filtered_image, caption="Brightness Boosted", use_container_width=True)
|
| 103 |
|
| 104 |
+
elif filter_option == "Contrast Boost":
|
| 105 |
+
enhancer = ImageEnhance.Contrast(image)
|
| 106 |
+
filtered_image = enhancer.enhance(2) # increase contrast
|
| 107 |
+
st.image(filtered_image, caption="Contrast Boosted", use_container_width=True)
|
| 108 |
|
| 109 |
+
elif filter_option == "Flip Horizontal":
|
| 110 |
+
filtered_image = image.transpose(Image.FLIP_LEFT_RIGHT)
|
| 111 |
+
st.image(filtered_image, caption="Flipped Horizontal", use_container_width=True)
|
| 112 |
|
| 113 |
+
elif filter_option == "Flip Vertical":
|
| 114 |
+
filtered_image = image.transpose(Image.FLIP_TOP_BOTTOM)
|
| 115 |
+
st.image(filtered_image, caption="Flipped Vertical", use_container_width=True)
|
| 116 |
|
| 117 |
+
elif filter_option == "Rotate 90°":
|
| 118 |
+
filtered_image = image.rotate(90)
|
| 119 |
+
st.image(filtered_image, caption="Rotated 90°", use_container_width=True)
|
| 120 |
|
| 121 |
+
else:
|
| 122 |
+
st.info("Select a filter to see output.")
|
| 123 |
|
|
|