Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from PIL import Image, ImageEnhance, ImageOps, ImageFilter
|
| 3 |
import numpy as np
|
| 4 |
from io import BytesIO
|
| 5 |
|
|
@@ -41,6 +41,28 @@ class ContrastFilter(ImageEditor):
|
|
| 41 |
return enhancer.enhance(self.factor)
|
| 42 |
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
# Blur Effect Class
|
| 45 |
class BlurFilter(ImageEditor):
|
| 46 |
def __init__(self, image, radius):
|
|
@@ -51,6 +73,44 @@ class BlurFilter(ImageEditor):
|
|
| 51 |
return self.image.filter(ImageFilter.GaussianBlur(self.radius))
|
| 52 |
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
# Border Class
|
| 55 |
class Border:
|
| 56 |
def __init__(self, image, border_type, color, thickness):
|
|
@@ -68,18 +128,34 @@ class Border:
|
|
| 68 |
|
| 69 |
if self.border_type == "Solid Color":
|
| 70 |
return ImageOps.expand(self.image, border=self.thickness, fill=border_color)
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
| 73 |
return self.image
|
| 74 |
|
| 75 |
-
|
| 76 |
-
def hex_to_rgb(hex_color):
|
| 77 |
hex_color = hex_color.lstrip('#')
|
| 78 |
if len(hex_color) == 6:
|
| 79 |
return tuple(int(hex_color[i:i + 2], 16) for i in (0, 2, 4))
|
| 80 |
else:
|
| 81 |
raise ValueError("Invalid hex color format")
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
# Main Application Class
|
| 85 |
class ImageApp:
|
|
@@ -96,7 +172,8 @@ class ImageApp:
|
|
| 96 |
def apply_filters(self):
|
| 97 |
if self.image:
|
| 98 |
# Sidebar Filters
|
| 99 |
-
filter_option = st.sidebar.selectbox("Choose a filter", ["None", "Grayscale", "Brightness", "Contrast",
|
|
|
|
| 100 |
if filter_option == "Grayscale":
|
| 101 |
self.image = GrayscaleFilter(self.image).apply()
|
| 102 |
elif filter_option == "Brightness":
|
|
@@ -105,13 +182,25 @@ class ImageApp:
|
|
| 105 |
elif filter_option == "Contrast":
|
| 106 |
contrast_factor = st.sidebar.slider("Adjust Contrast", 0.1, 2.0, 1.0)
|
| 107 |
self.image = ContrastFilter(self.image, contrast_factor).apply()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
elif filter_option == "Blur":
|
| 109 |
blur_radius = st.sidebar.slider("Blur Radius", 1, 10, 2)
|
| 110 |
self.image = BlurFilter(self.image, blur_radius).apply()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
def add_border(self):
|
| 113 |
if self.image:
|
| 114 |
-
border_type = st.sidebar.selectbox("Choose Border Type", ["None", "Solid Color"])
|
| 115 |
if border_type != "None":
|
| 116 |
border_color = st.sidebar.color_picker("Select Border Color", "#b33c3c")
|
| 117 |
border_thickness = st.sidebar.slider("Border Thickness", 1, 20, 5)
|
|
@@ -136,7 +225,7 @@ class ImageApp:
|
|
| 136 |
|
| 137 |
# Main function to run the app
|
| 138 |
def main():
|
| 139 |
-
st.title("
|
| 140 |
app = ImageApp()
|
| 141 |
|
| 142 |
# Load the image
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from PIL import Image, ImageEnhance, ImageOps, ImageFilter, ImageDraw, ImageFont
|
| 3 |
import numpy as np
|
| 4 |
from io import BytesIO
|
| 5 |
|
|
|
|
| 41 |
return enhancer.enhance(self.factor)
|
| 42 |
|
| 43 |
|
| 44 |
+
# Sharpness Adjustment Class
|
| 45 |
+
class SharpnessFilter(ImageEditor):
|
| 46 |
+
def __init__(self, image, factor):
|
| 47 |
+
super().__init__(image)
|
| 48 |
+
self.factor = factor
|
| 49 |
+
|
| 50 |
+
def apply(self):
|
| 51 |
+
enhancer = ImageEnhance.Sharpness(self.image)
|
| 52 |
+
return enhancer.enhance(self.factor)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
# Saturation Adjustment Class
|
| 56 |
+
class SaturationFilter(ImageEditor):
|
| 57 |
+
def __init__(self, image, factor):
|
| 58 |
+
super().__init__(image)
|
| 59 |
+
self.factor = factor
|
| 60 |
+
|
| 61 |
+
def apply(self):
|
| 62 |
+
enhancer = ImageEnhance.Color(self.image)
|
| 63 |
+
return enhancer.enhance(self.factor)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
# Blur Effect Class
|
| 67 |
class BlurFilter(ImageEditor):
|
| 68 |
def __init__(self, image, radius):
|
|
|
|
| 73 |
return self.image.filter(ImageFilter.GaussianBlur(self.radius))
|
| 74 |
|
| 75 |
|
| 76 |
+
# Sepia Filter Class
|
| 77 |
+
class SepiaFilter(ImageEditor):
|
| 78 |
+
def apply(self):
|
| 79 |
+
width, height = self.image.size
|
| 80 |
+
pixels = self.image.load()
|
| 81 |
+
for py in range(height):
|
| 82 |
+
for px in range(width):
|
| 83 |
+
r, g, b = self.image.getpixel((px, py))
|
| 84 |
+
|
| 85 |
+
tr = int(0.393 * r + 0.769 * g + 0.189 * b)
|
| 86 |
+
tg = int(0.349 * r + 0.686 * g + 0.168 * b)
|
| 87 |
+
tb = int(0.272 * r + 0.534 * g + 0.131 * b)
|
| 88 |
+
|
| 89 |
+
# Apply limits to avoid overflow
|
| 90 |
+
if tr > 255:
|
| 91 |
+
tr = 255
|
| 92 |
+
if tg > 255:
|
| 93 |
+
tg = 255
|
| 94 |
+
if tb > 255:
|
| 95 |
+
tb = 255
|
| 96 |
+
|
| 97 |
+
pixels[px, py] = (tr, tg, tb)
|
| 98 |
+
|
| 99 |
+
return self.image
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
# Emboss Filter Class
|
| 103 |
+
class EmbossFilter(ImageEditor):
|
| 104 |
+
def apply(self):
|
| 105 |
+
return self.image.filter(ImageFilter.EMBOSS)
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
# Invert Filter Class
|
| 109 |
+
class InvertFilter(ImageEditor):
|
| 110 |
+
def apply(self):
|
| 111 |
+
return ImageOps.invert(self.image.convert('RGB'))
|
| 112 |
+
|
| 113 |
+
|
| 114 |
# Border Class
|
| 115 |
class Border:
|
| 116 |
def __init__(self, image, border_type, color, thickness):
|
|
|
|
| 128 |
|
| 129 |
if self.border_type == "Solid Color":
|
| 130 |
return ImageOps.expand(self.image, border=self.thickness, fill=border_color)
|
| 131 |
+
elif self.border_type == "Shadow":
|
| 132 |
+
return self.add_shadow_border()
|
| 133 |
+
elif self.border_type == "Rounded":
|
| 134 |
+
return self.add_rounded_border()
|
| 135 |
return self.image
|
| 136 |
|
| 137 |
+
def hex_to_rgb(self, hex_color):
|
|
|
|
| 138 |
hex_color = hex_color.lstrip('#')
|
| 139 |
if len(hex_color) == 6:
|
| 140 |
return tuple(int(hex_color[i:i + 2], 16) for i in (0, 2, 4))
|
| 141 |
else:
|
| 142 |
raise ValueError("Invalid hex color format")
|
| 143 |
|
| 144 |
+
def add_shadow_border(self):
|
| 145 |
+
shadow = Image.new("RGBA", (self.image.width + 20, self.image.height + 20), (0, 0, 0, 0))
|
| 146 |
+
shadow.paste(self.image, (10, 10))
|
| 147 |
+
return shadow.convert("RGB")
|
| 148 |
+
|
| 149 |
+
def add_rounded_border(self):
|
| 150 |
+
radius = 20
|
| 151 |
+
img_with_border = self.image.copy()
|
| 152 |
+
img_with_border = img_with_border.convert("RGBA")
|
| 153 |
+
mask = Image.new("L", img_with_border.size, 255)
|
| 154 |
+
draw = ImageDraw.Draw(mask)
|
| 155 |
+
draw.rounded_rectangle([0, 0, img_with_border.width, img_with_border.height], radius, fill=0)
|
| 156 |
+
img_with_border.putalpha(mask)
|
| 157 |
+
return img_with_border.convert("RGB")
|
| 158 |
+
|
| 159 |
|
| 160 |
# Main Application Class
|
| 161 |
class ImageApp:
|
|
|
|
| 172 |
def apply_filters(self):
|
| 173 |
if self.image:
|
| 174 |
# Sidebar Filters
|
| 175 |
+
filter_option = st.sidebar.selectbox("Choose a filter", ["None", "Grayscale", "Brightness", "Contrast",
|
| 176 |
+
"Sharpness", "Saturation", "Blur", "Sepia", "Emboss", "Invert"])
|
| 177 |
if filter_option == "Grayscale":
|
| 178 |
self.image = GrayscaleFilter(self.image).apply()
|
| 179 |
elif filter_option == "Brightness":
|
|
|
|
| 182 |
elif filter_option == "Contrast":
|
| 183 |
contrast_factor = st.sidebar.slider("Adjust Contrast", 0.1, 2.0, 1.0)
|
| 184 |
self.image = ContrastFilter(self.image, contrast_factor).apply()
|
| 185 |
+
elif filter_option == "Sharpness":
|
| 186 |
+
sharpness_factor = st.sidebar.slider("Adjust Sharpness", 0.1, 2.0, 1.0)
|
| 187 |
+
self.image = SharpnessFilter(self.image, sharpness_factor).apply()
|
| 188 |
+
elif filter_option == "Saturation":
|
| 189 |
+
saturation_factor = st.sidebar.slider("Adjust Saturation", 0.1, 2.0, 1.0)
|
| 190 |
+
self.image = SaturationFilter(self.image, saturation_factor).apply()
|
| 191 |
elif filter_option == "Blur":
|
| 192 |
blur_radius = st.sidebar.slider("Blur Radius", 1, 10, 2)
|
| 193 |
self.image = BlurFilter(self.image, blur_radius).apply()
|
| 194 |
+
elif filter_option == "Sepia":
|
| 195 |
+
self.image = SepiaFilter(self.image).apply()
|
| 196 |
+
elif filter_option == "Emboss":
|
| 197 |
+
self.image = EmbossFilter(self.image).apply()
|
| 198 |
+
elif filter_option == "Invert":
|
| 199 |
+
self.image = InvertFilter(self.image).apply()
|
| 200 |
|
| 201 |
def add_border(self):
|
| 202 |
if self.image:
|
| 203 |
+
border_type = st.sidebar.selectbox("Choose Border Type", ["None", "Solid Color", "Shadow", "Rounded"])
|
| 204 |
if border_type != "None":
|
| 205 |
border_color = st.sidebar.color_picker("Select Border Color", "#b33c3c")
|
| 206 |
border_thickness = st.sidebar.slider("Border Thickness", 1, 20, 5)
|
|
|
|
| 225 |
|
| 226 |
# Main function to run the app
|
| 227 |
def main():
|
| 228 |
+
st.title("Modernized Image Editor")
|
| 229 |
app = ImageApp()
|
| 230 |
|
| 231 |
# Load the image
|