Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,131 +3,156 @@ from PIL import Image, ImageEnhance, ImageOps, ImageFilter
|
|
| 3 |
import numpy as np
|
| 4 |
from io import BytesIO
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
raise
|
| 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 |
def main():
|
| 80 |
-
st.title("Image Editor")
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
brightness_factor = st.sidebar.slider("Adjust Brightness", 0.1, 2.0, 1.0)
|
| 98 |
-
image = adjust_brightness(image, brightness_factor)
|
| 99 |
-
elif filter_option == "Contrast":
|
| 100 |
-
contrast_factor = st.sidebar.slider("Adjust Contrast", 0.1, 2.0, 1.0)
|
| 101 |
-
image = adjust_contrast(image, contrast_factor)
|
| 102 |
-
elif filter_option == "Blur":
|
| 103 |
-
blur_radius = st.sidebar.slider("Blur Radius", 1, 10, 2)
|
| 104 |
-
image = apply_blur(image, blur_radius)
|
| 105 |
-
|
| 106 |
-
# Border options
|
| 107 |
-
border_type = st.sidebar.selectbox("Choose Border Type", [
|
| 108 |
-
"None", "Solid Color", "Dotted", "Dashed", "Double", "Inset", "Outset",
|
| 109 |
-
"Rounded Corner", "Shadowed", "Polaroid", "Metallic", "Wooden", "Textured",
|
| 110 |
-
"Neon", "Polka Dots", "Stripe", "Sketch", "Lace"
|
| 111 |
-
])
|
| 112 |
-
if border_type != "None":
|
| 113 |
-
border_color = st.sidebar.color_picker("Select Border Color", "#b33c3c")
|
| 114 |
-
border_thickness = st.sidebar.slider("Border Thickness", 1, 20, 5)
|
| 115 |
-
image = add_border(image, border_type, border_color, border_thickness)
|
| 116 |
-
|
| 117 |
-
# Display the original and edited images side by side
|
| 118 |
-
col1, col2 = st.columns(2)
|
| 119 |
-
|
| 120 |
-
with col1:
|
| 121 |
-
st.image(uploaded_image, caption='Original Image', use_container_width=True)
|
| 122 |
-
|
| 123 |
-
with col2:
|
| 124 |
-
st.image(image, caption='Edited Image', use_container_width=True)
|
| 125 |
-
|
| 126 |
-
# Download button for edited image
|
| 127 |
-
buffered = BytesIO()
|
| 128 |
-
image.save(buffered, format="PNG")
|
| 129 |
-
buffered.seek(0)
|
| 130 |
-
st.sidebar.download_button("Download Edited Image", buffered, "edited_image.png", "image/png")
|
| 131 |
|
| 132 |
|
| 133 |
if __name__ == "__main__":
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from io import BytesIO
|
| 5 |
|
| 6 |
+
|
| 7 |
+
# Abstract Class for Image Editor
|
| 8 |
+
class ImageEditor:
|
| 9 |
+
def __init__(self, image):
|
| 10 |
+
self.image = image
|
| 11 |
+
|
| 12 |
+
def apply(self):
|
| 13 |
+
raise NotImplementedError("Subclasses should implement this method")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# Grayscale Filter Class
|
| 17 |
+
class GrayscaleFilter(ImageEditor):
|
| 18 |
+
def apply(self):
|
| 19 |
+
return self.image.convert("L")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# Brightness Adjustment Class
|
| 23 |
+
class BrightnessFilter(ImageEditor):
|
| 24 |
+
def __init__(self, image, factor):
|
| 25 |
+
super().__init__(image)
|
| 26 |
+
self.factor = factor
|
| 27 |
+
|
| 28 |
+
def apply(self):
|
| 29 |
+
enhancer = ImageEnhance.Brightness(self.image)
|
| 30 |
+
return enhancer.enhance(self.factor)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# Contrast Adjustment Class
|
| 34 |
+
class ContrastFilter(ImageEditor):
|
| 35 |
+
def __init__(self, image, factor):
|
| 36 |
+
super().__init__(image)
|
| 37 |
+
self.factor = factor
|
| 38 |
+
|
| 39 |
+
def apply(self):
|
| 40 |
+
enhancer = ImageEnhance.Contrast(self.image)
|
| 41 |
+
return enhancer.enhance(self.factor)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# Blur Effect Class
|
| 45 |
+
class BlurFilter(ImageEditor):
|
| 46 |
+
def __init__(self, image, radius):
|
| 47 |
+
super().__init__(image)
|
| 48 |
+
self.radius = radius
|
| 49 |
+
|
| 50 |
+
def apply(self):
|
| 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):
|
| 57 |
+
self.image = image
|
| 58 |
+
self.border_type = border_type
|
| 59 |
+
self.color = color
|
| 60 |
+
self.thickness = thickness
|
| 61 |
+
|
| 62 |
+
def add_border(self):
|
| 63 |
+
try:
|
| 64 |
+
border_color = self.hex_to_rgb(self.color)
|
| 65 |
+
except ValueError:
|
| 66 |
+
st.error("Invalid color format. Please use Hex color.")
|
| 67 |
+
return self.image
|
| 68 |
+
|
| 69 |
+
if self.border_type == "Solid Color":
|
| 70 |
+
return ImageOps.expand(self.image, border=self.thickness, fill=border_color)
|
| 71 |
+
# Add other border types if needed
|
| 72 |
+
|
| 73 |
+
return self.image
|
| 74 |
+
|
| 75 |
+
@staticmethod
|
| 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:
|
| 86 |
+
def __init__(self):
|
| 87 |
+
self.image = None
|
| 88 |
+
self.stored_image = None
|
| 89 |
+
|
| 90 |
+
def load_image(self):
|
| 91 |
+
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 92 |
+
if uploaded_image:
|
| 93 |
+
self.image = Image.open(uploaded_image)
|
| 94 |
+
self.stored_image = self.image.copy()
|
| 95 |
+
|
| 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", "Blur"])
|
| 100 |
+
if filter_option == "Grayscale":
|
| 101 |
+
self.image = GrayscaleFilter(self.image).apply()
|
| 102 |
+
elif filter_option == "Brightness":
|
| 103 |
+
brightness_factor = st.sidebar.slider("Adjust Brightness", 0.1, 2.0, 1.0)
|
| 104 |
+
self.image = BrightnessFilter(self.image, brightness_factor).apply()
|
| 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)
|
| 118 |
+
border = Border(self.image, border_type, border_color, border_thickness)
|
| 119 |
+
self.image = border.add_border()
|
| 120 |
+
|
| 121 |
+
def display_images(self):
|
| 122 |
+
if self.image:
|
| 123 |
+
col1, col2 = st.columns(2)
|
| 124 |
+
with col1:
|
| 125 |
+
st.image(self.stored_image, caption="Original Image", use_container_width=True)
|
| 126 |
+
with col2:
|
| 127 |
+
st.image(self.image, caption="Edited Image", use_container_width=True)
|
| 128 |
+
|
| 129 |
+
def download_button(self):
|
| 130 |
+
if self.image:
|
| 131 |
+
buffered = BytesIO()
|
| 132 |
+
self.image.save(buffered, format="PNG")
|
| 133 |
+
buffered.seek(0)
|
| 134 |
+
st.sidebar.download_button("Download Edited Image", buffered, "edited_image.png", "image/png")
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
# Main function to run the app
|
| 138 |
def main():
|
| 139 |
+
st.title("Modern Image Editor")
|
| 140 |
+
app = ImageApp()
|
| 141 |
+
|
| 142 |
+
# Load the image
|
| 143 |
+
app.load_image()
|
| 144 |
+
|
| 145 |
+
# Apply filters
|
| 146 |
+
app.apply_filters()
|
| 147 |
+
|
| 148 |
+
# Add borders if selected
|
| 149 |
+
app.add_border()
|
| 150 |
+
|
| 151 |
+
# Display images side by side
|
| 152 |
+
app.display_images()
|
| 153 |
+
|
| 154 |
+
# Provide download option
|
| 155 |
+
app.download_button()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
|
| 158 |
if __name__ == "__main__":
|