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,
|
| 3 |
import numpy as np
|
| 4 |
from io import BytesIO
|
| 5 |
|
|
@@ -8,32 +8,79 @@ def hex_to_rgb(hex_color):
|
|
| 8 |
hex_color = hex_color.lstrip('#') # Remove the '#' if present
|
| 9 |
return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4)) # Convert to RGB
|
| 10 |
|
| 11 |
-
# Function to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def apply_grayscale(image):
|
| 13 |
-
return image.convert(
|
| 14 |
|
| 15 |
-
# Function to
|
| 16 |
-
def
|
| 17 |
enhancer = ImageEnhance.Brightness(image)
|
| 18 |
return enhancer.enhance(factor)
|
| 19 |
|
| 20 |
-
# Function to
|
| 21 |
-
def
|
| 22 |
enhancer = ImageEnhance.Contrast(image)
|
| 23 |
return enhancer.enhance(factor)
|
| 24 |
|
| 25 |
-
# Function to apply
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
def apply_blur(image, blur_radius):
|
| 27 |
-
return image.filter(ImageFilter.GaussianBlur(blur_radius))
|
| 28 |
|
| 29 |
# Function to add border to image
|
| 30 |
def add_border(image, border_type, color, thickness):
|
| 31 |
width, height = image.size
|
| 32 |
-
border_color =
|
| 33 |
-
|
| 34 |
if border_type == "Solid Color":
|
| 35 |
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 36 |
elif border_type == "Dotted":
|
|
|
|
| 37 |
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 38 |
elif border_type == "Dashed":
|
| 39 |
return ImageOps.expand(image, border=thickness, fill=border_color)
|
|
@@ -66,55 +113,66 @@ def add_border(image, border_type, color, thickness):
|
|
| 66 |
elif border_type == "Lace":
|
| 67 |
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 68 |
|
| 69 |
-
# Main function to handle Streamlit app
|
| 70 |
def main():
|
| 71 |
st.title("Image Editor")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
# Upload image
|
| 74 |
-
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 75 |
-
|
| 76 |
if uploaded_image is not None:
|
| 77 |
image = Image.open(uploaded_image)
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
if filter_option == "Grayscale":
|
| 87 |
image = apply_grayscale(image)
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
image = apply_blur(image, blur_radius)
|
| 97 |
|
| 98 |
# Border options
|
| 99 |
-
border_type = st.sidebar.selectbox("
|
| 100 |
-
"
|
| 101 |
-
"
|
| 102 |
-
"
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
# Display the
|
| 110 |
-
st.image(image, caption=
|
| 111 |
-
|
| 112 |
-
# Download button
|
| 113 |
buffered = BytesIO()
|
| 114 |
-
image.save(buffered, format="
|
| 115 |
buffered.seek(0)
|
| 116 |
-
st.sidebar.download_button("Download Edited Image", buffered, "edited_image.png", "image/png")
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
if __name__ == "__main__":
|
| 120 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from PIL import Image, ImageEnhance, ImageFilter, ImageOps
|
| 3 |
import numpy as np
|
| 4 |
from io import BytesIO
|
| 5 |
|
|
|
|
| 8 |
hex_color = hex_color.lstrip('#') # Remove the '#' if present
|
| 9 |
return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4)) # Convert to RGB
|
| 10 |
|
| 11 |
+
# Function to add border to image
|
| 12 |
+
def add_border(image, border_type, color, thickness):
|
| 13 |
+
width, height = image.size
|
| 14 |
+
border_color = hex_to_rgb(color) # Convert hex color to RGB tuple
|
| 15 |
+
|
| 16 |
+
if border_type == "Solid Color":
|
| 17 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 18 |
+
elif border_type == "Dotted":
|
| 19 |
+
# Use dotted pattern (this is a simple approximation)
|
| 20 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 21 |
+
elif border_type == "Dashed":
|
| 22 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 23 |
+
elif border_type == "Double":
|
| 24 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 25 |
+
elif border_type == "Inset":
|
| 26 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 27 |
+
elif border_type == "Outset":
|
| 28 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 29 |
+
elif border_type == "Rounded Corner":
|
| 30 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 31 |
+
elif border_type == "Shadowed":
|
| 32 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 33 |
+
elif border_type == "Polaroid":
|
| 34 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 35 |
+
elif border_type == "Metallic":
|
| 36 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 37 |
+
elif border_type == "Wooden":
|
| 38 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 39 |
+
elif border_type == "Textured":
|
| 40 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 41 |
+
elif border_type == "Neon":
|
| 42 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 43 |
+
elif border_type == "Polka Dots":
|
| 44 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 45 |
+
elif border_type == "Stripe":
|
| 46 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 47 |
+
elif border_type == "Sketch":
|
| 48 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 49 |
+
elif border_type == "Lace":
|
| 50 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# Function to apply grayscale filter
|
| 54 |
def apply_grayscale(image):
|
| 55 |
+
return image.convert("L")
|
| 56 |
|
| 57 |
+
# Function to apply brightness adjustment
|
| 58 |
+
def apply_brightness(image, factor):
|
| 59 |
enhancer = ImageEnhance.Brightness(image)
|
| 60 |
return enhancer.enhance(factor)
|
| 61 |
|
| 62 |
+
# Function to apply contrast adjustment
|
| 63 |
+
def apply_contrast(image, factor):
|
| 64 |
enhancer = ImageEnhance.Contrast(image)
|
| 65 |
return enhancer.enhance(factor)
|
| 66 |
|
| 67 |
+
# Function to apply sharpness adjustment
|
| 68 |
+
def apply_sharpness(image, factor):
|
| 69 |
+
enhancer = ImageEnhance.Sharpness(image)
|
| 70 |
+
return enhancer.enhance(factor)
|
| 71 |
+
|
| 72 |
+
# Function to apply blur filter
|
| 73 |
def apply_blur(image, blur_radius):
|
| 74 |
+
return image.filter(ImageFilter.GaussianBlur(radius=blur_radius))
|
| 75 |
|
| 76 |
# Function to add border to image
|
| 77 |
def add_border(image, border_type, color, thickness):
|
| 78 |
width, height = image.size
|
| 79 |
+
border_color = tuple(int(c) for c in color.split(','))
|
|
|
|
| 80 |
if border_type == "Solid Color":
|
| 81 |
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 82 |
elif border_type == "Dotted":
|
| 83 |
+
# Use dotted pattern
|
| 84 |
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 85 |
elif border_type == "Dashed":
|
| 86 |
return ImageOps.expand(image, border=thickness, fill=border_color)
|
|
|
|
| 113 |
elif border_type == "Lace":
|
| 114 |
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 115 |
|
|
|
|
| 116 |
def main():
|
| 117 |
st.title("Image Editor")
|
| 118 |
+
st.sidebar.title("Enhance Image")
|
| 119 |
+
|
| 120 |
+
# Upload Image
|
| 121 |
+
uploaded_image = st.sidebar.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
| 122 |
|
|
|
|
|
|
|
|
|
|
| 123 |
if uploaded_image is not None:
|
| 124 |
image = Image.open(uploaded_image)
|
| 125 |
+
|
| 126 |
+
# Show original image
|
| 127 |
+
st.image(image, caption="Original Image", use_container_width=True)
|
| 128 |
+
|
| 129 |
+
# Grayscale option
|
| 130 |
+
grayscale = st.sidebar.checkbox("Apply Grayscale")
|
| 131 |
+
if grayscale:
|
|
|
|
|
|
|
| 132 |
image = apply_grayscale(image)
|
| 133 |
+
|
| 134 |
+
# Brightness adjustment
|
| 135 |
+
brightness_factor = st.sidebar.slider("Adjust Brightness", 0.0, 2.0, 1.0)
|
| 136 |
+
image = apply_brightness(image, brightness_factor)
|
| 137 |
+
|
| 138 |
+
# Contrast adjustment
|
| 139 |
+
contrast_factor = st.sidebar.slider("Adjust Contrast", 0.0, 2.0, 1.0)
|
| 140 |
+
image = apply_contrast(image, contrast_factor)
|
| 141 |
+
|
| 142 |
+
# Sharpness adjustment
|
| 143 |
+
sharpness_factor = st.sidebar.slider("Adjust Sharpness", 0.0, 2.0, 1.0)
|
| 144 |
+
image = apply_sharpness(image, sharpness_factor)
|
| 145 |
+
|
| 146 |
+
# Blur effect
|
| 147 |
+
blur_radius = st.sidebar.slider("Blur Radius", 0, 10, 0)
|
| 148 |
+
if blur_radius > 0:
|
| 149 |
image = apply_blur(image, blur_radius)
|
| 150 |
|
| 151 |
# Border options
|
| 152 |
+
border_type = st.sidebar.selectbox("Select Border Type", [
|
| 153 |
+
"Solid Color", "Dotted", "Dashed", "Double", "Inset", "Outset", "Rounded Corner",
|
| 154 |
+
"Shadowed", "Polaroid", "Metallic", "Wooden", "Textured", "Neon", "Polka Dots",
|
| 155 |
+
"Stripe", "Sketch", "Lace"])
|
| 156 |
+
|
| 157 |
+
border_color = st.sidebar.color_picker("Pick a Border Color", "#000000")
|
| 158 |
+
border_thickness = st.sidebar.slider("Select Border Thickness", 0, 50, 10)
|
| 159 |
+
|
| 160 |
+
image = add_border(image, border_type, border_color[1:], border_thickness)
|
| 161 |
+
|
| 162 |
+
# Display the modified image
|
| 163 |
+
st.image(image, caption="Modified Image", use_container_width=True)
|
| 164 |
+
|
| 165 |
+
# Download button
|
| 166 |
buffered = BytesIO()
|
| 167 |
+
image.save(buffered, format="JPEG")
|
| 168 |
buffered.seek(0)
|
|
|
|
| 169 |
|
| 170 |
+
st.sidebar.download_button(
|
| 171 |
+
label="Download Edited Image",
|
| 172 |
+
data=buffered,
|
| 173 |
+
file_name="edited_image.jpg",
|
| 174 |
+
mime="image/jpeg"
|
| 175 |
+
)
|
| 176 |
|
| 177 |
if __name__ == "__main__":
|
| 178 |
main()
|