Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,228 +1,131 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image, ImageEnhance, ImageFilter, ImageOps
|
| 3 |
import numpy as np
|
| 4 |
-
import
|
| 5 |
-
import io
|
| 6 |
|
| 7 |
# Function to apply grayscale filter
|
| 8 |
def apply_grayscale(image):
|
| 9 |
return image.convert("L")
|
| 10 |
|
| 11 |
-
# Function to
|
| 12 |
-
def
|
| 13 |
enhancer = ImageEnhance.Brightness(image)
|
| 14 |
return enhancer.enhance(factor)
|
| 15 |
|
| 16 |
-
# Function to
|
| 17 |
-
def
|
| 18 |
enhancer = ImageEnhance.Contrast(image)
|
| 19 |
return enhancer.enhance(factor)
|
| 20 |
|
| 21 |
-
# Function to
|
| 22 |
-
def adjust_saturation(image, factor):
|
| 23 |
-
enhancer = ImageEnhance.Color(image)
|
| 24 |
-
return enhancer.enhance(factor)
|
| 25 |
-
|
| 26 |
-
# Function to apply sharpness enhancement
|
| 27 |
def apply_sharpness(image, factor):
|
| 28 |
enhancer = ImageEnhance.Sharpness(image)
|
| 29 |
return enhancer.enhance(factor)
|
| 30 |
|
| 31 |
-
# Function to apply
|
| 32 |
-
def
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
[0.272, 0.534, 0.131]])
|
| 38 |
-
img = cv2.transform(img, sepia_filter)
|
| 39 |
-
img = np.clip(img, 0, 255)
|
| 40 |
-
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 41 |
-
return Image.fromarray(img)
|
| 42 |
-
|
| 43 |
-
# Function to apply vignette effect
|
| 44 |
-
def apply_vignette(image):
|
| 45 |
-
img = np.array(image)
|
| 46 |
-
rows, cols = img.shape[:2]
|
| 47 |
-
X_resultant_kernel = cv2.getGaussianKernel(cols, cols / 3)
|
| 48 |
-
Y_resultant_kernel = cv2.getGaussianKernel(rows, rows / 3)
|
| 49 |
-
resultant_kernel = Y_resultant_kernel * X_resultant_kernel.T
|
| 50 |
-
mask = 255 * resultant_kernel / np.linalg.norm(resultant_kernel)
|
| 51 |
-
img = np.multiply(img, mask[..., np.newaxis].astype(img.dtype))
|
| 52 |
-
return Image.fromarray(img)
|
| 53 |
-
|
| 54 |
-
# Function to apply blur effect
|
| 55 |
-
def apply_blur(image, blur_factor):
|
| 56 |
-
return image.filter(ImageFilter.GaussianBlur(radius=blur_factor))
|
| 57 |
-
|
| 58 |
-
# Function to apply edge detection
|
| 59 |
-
def apply_edge_detection(image):
|
| 60 |
-
img = np.array(image)
|
| 61 |
-
edges = cv2.Canny(img, 100, 200)
|
| 62 |
-
return Image.fromarray(edges)
|
| 63 |
-
|
| 64 |
-
# Function to add border
|
| 65 |
-
def add_border(image, border_color, border_width):
|
| 66 |
-
# Adding border using PIL ImageOps
|
| 67 |
-
return ImageOps.expand(image, border=border_width, fill=border_color)
|
| 68 |
-
|
| 69 |
-
# Function to add frame
|
| 70 |
-
def add_frame(image, frame_style="Simple"):
|
| 71 |
width, height = image.size
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
return ImageOps.expand(image, border=
|
| 80 |
-
elif
|
| 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 |
-
image_with_outer_border = ImageOps.expand(image, border=outer_width, fill="black")
|
| 110 |
-
return ImageOps.expand(image_with_outer_border, border=inner_width, fill="white")
|
| 111 |
-
elif frame_style == "Metallic":
|
| 112 |
-
# Add metallic frame (gold, silver, etc.)
|
| 113 |
-
frame_width = 40
|
| 114 |
-
return ImageOps.expand(image, border=frame_width, fill="gold")
|
| 115 |
-
elif frame_style == "Heart-Shaped":
|
| 116 |
-
# Add heart-shaped frame
|
| 117 |
-
image_with_frame = image.resize((450, 450)) # Adjust for better fit
|
| 118 |
-
return image_with_frame.convert("RGBA")
|
| 119 |
-
elif frame_style == "Circular":
|
| 120 |
-
# Add circular frame
|
| 121 |
-
image_with_frame = image.resize((450, 450)) # Adjust for better fit
|
| 122 |
-
return image_with_frame.convert("RGBA")
|
| 123 |
-
elif frame_style == "Wooden":
|
| 124 |
-
# Add wooden frame
|
| 125 |
-
frame_width = 50
|
| 126 |
-
return ImageOps.expand(image, border=frame_width, fill="saddlebrown")
|
| 127 |
-
elif frame_style == "Neon Glow":
|
| 128 |
-
# Add neon glow frame
|
| 129 |
-
frame_width = 50
|
| 130 |
-
neon_image = Image.new("RGBA", (image.width + 2 * frame_width, image.height + 2 * frame_width), (0, 255, 255, 255))
|
| 131 |
-
neon_image.paste(image, (frame_width, frame_width))
|
| 132 |
-
return neon_image.convert("RGB")
|
| 133 |
-
elif frame_style == "Gradient":
|
| 134 |
-
# Add gradient frame
|
| 135 |
-
frame_width = 40
|
| 136 |
-
return ImageOps.expand(image, border=frame_width, fill="gradient")
|
| 137 |
-
elif frame_style == "Geometric":
|
| 138 |
-
# Add geometric frame (triangles, hexagons, etc.)
|
| 139 |
-
frame_width = 30
|
| 140 |
-
return ImageOps.expand(image, border=frame_width, fill="purple")
|
| 141 |
-
|
| 142 |
-
# Function to convert PIL image to bytes for download
|
| 143 |
-
def pil_to_bytes(image, format="JPEG"):
|
| 144 |
-
img_byte_arr = io.BytesIO()
|
| 145 |
-
image.save(img_byte_arr, format=format)
|
| 146 |
-
img_byte_arr.seek(0)
|
| 147 |
-
return img_byte_arr
|
| 148 |
-
|
| 149 |
-
# Streamlit app
|
| 150 |
def main():
|
| 151 |
-
st.title("
|
|
|
|
| 152 |
|
| 153 |
-
# Upload
|
| 154 |
-
uploaded_image = st.file_uploader("Upload an image", type=["
|
| 155 |
|
| 156 |
if uploaded_image is not None:
|
| 157 |
-
# Open the uploaded image using PIL
|
| 158 |
image = Image.open(uploaded_image)
|
| 159 |
|
| 160 |
-
#
|
| 161 |
-
|
| 162 |
|
| 163 |
-
#
|
| 164 |
-
st.sidebar.
|
| 165 |
-
|
| 166 |
-
filter_option = st.sidebar.selectbox("Choose a filter", ("None", "Grayscale", "Brightness", "Contrast", "Saturation", "Sharpness", "Sepia", "Vignette", "Blur", "Edge Detection"))
|
| 167 |
-
|
| 168 |
-
if filter_option == "Grayscale":
|
| 169 |
image = apply_grayscale(image)
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
with col1:
|
| 214 |
-
st.image(original_image_resized, caption="Original Image", use_container_width=True)
|
| 215 |
-
|
| 216 |
-
# Display the modified image in the second column
|
| 217 |
-
with col2:
|
| 218 |
-
st.image(modified_image_resized, caption="Modified Image", use_container_width=True)
|
| 219 |
-
|
| 220 |
-
# Convert the edited image to bytes for download
|
| 221 |
-
img_bytes = pil_to_bytes(image)
|
| 222 |
-
|
| 223 |
-
# Button to download the edited image below the modified image
|
| 224 |
-
with col2:
|
| 225 |
-
st.download_button("Download Edited Image", img_bytes, "edited_image.jpg", "image/jpeg")
|
| 226 |
|
| 227 |
if __name__ == "__main__":
|
| 228 |
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 |
|
| 6 |
# Function to apply grayscale filter
|
| 7 |
def apply_grayscale(image):
|
| 8 |
return image.convert("L")
|
| 9 |
|
| 10 |
+
# Function to apply brightness adjustment
|
| 11 |
+
def apply_brightness(image, factor):
|
| 12 |
enhancer = ImageEnhance.Brightness(image)
|
| 13 |
return enhancer.enhance(factor)
|
| 14 |
|
| 15 |
+
# Function to apply contrast adjustment
|
| 16 |
+
def apply_contrast(image, factor):
|
| 17 |
enhancer = ImageEnhance.Contrast(image)
|
| 18 |
return enhancer.enhance(factor)
|
| 19 |
|
| 20 |
+
# Function to apply sharpness adjustment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def apply_sharpness(image, factor):
|
| 22 |
enhancer = ImageEnhance.Sharpness(image)
|
| 23 |
return enhancer.enhance(factor)
|
| 24 |
|
| 25 |
+
# Function to apply blur filter
|
| 26 |
+
def apply_blur(image, blur_radius):
|
| 27 |
+
return image.filter(ImageFilter.GaussianBlur(radius=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 = tuple(int(c) for c in color.split(','))
|
| 33 |
+
if border_type == "Solid Color":
|
| 34 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 35 |
+
elif border_type == "Dotted":
|
| 36 |
+
# Use dotted pattern
|
| 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)
|
| 40 |
+
elif border_type == "Double":
|
| 41 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 42 |
+
elif border_type == "Inset":
|
| 43 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 44 |
+
elif border_type == "Outset":
|
| 45 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 46 |
+
elif border_type == "Rounded Corner":
|
| 47 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 48 |
+
elif border_type == "Shadowed":
|
| 49 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 50 |
+
elif border_type == "Polaroid":
|
| 51 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 52 |
+
elif border_type == "Metallic":
|
| 53 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 54 |
+
elif border_type == "Wooden":
|
| 55 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 56 |
+
elif border_type == "Textured":
|
| 57 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 58 |
+
elif border_type == "Neon":
|
| 59 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 60 |
+
elif border_type == "Polka Dots":
|
| 61 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 62 |
+
elif border_type == "Stripe":
|
| 63 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 64 |
+
elif border_type == "Sketch":
|
| 65 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 66 |
+
elif border_type == "Lace":
|
| 67 |
+
return ImageOps.expand(image, border=thickness, fill=border_color)
|
| 68 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
def main():
|
| 70 |
+
st.title("Image Editor")
|
| 71 |
+
st.sidebar.title("Enhance Image")
|
| 72 |
|
| 73 |
+
# Upload Image
|
| 74 |
+
uploaded_image = st.sidebar.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
| 75 |
|
| 76 |
if uploaded_image is not None:
|
|
|
|
| 77 |
image = Image.open(uploaded_image)
|
| 78 |
|
| 79 |
+
# Show original image
|
| 80 |
+
st.image(image, caption="Original Image", use_container_width=True)
|
| 81 |
|
| 82 |
+
# Grayscale option
|
| 83 |
+
grayscale = st.sidebar.checkbox("Apply Grayscale")
|
| 84 |
+
if grayscale:
|
|
|
|
|
|
|
|
|
|
| 85 |
image = apply_grayscale(image)
|
| 86 |
+
|
| 87 |
+
# Brightness adjustment
|
| 88 |
+
brightness_factor = st.sidebar.slider("Adjust Brightness", 0.0, 2.0, 1.0)
|
| 89 |
+
image = apply_brightness(image, brightness_factor)
|
| 90 |
+
|
| 91 |
+
# Contrast adjustment
|
| 92 |
+
contrast_factor = st.sidebar.slider("Adjust Contrast", 0.0, 2.0, 1.0)
|
| 93 |
+
image = apply_contrast(image, contrast_factor)
|
| 94 |
+
|
| 95 |
+
# Sharpness adjustment
|
| 96 |
+
sharpness_factor = st.sidebar.slider("Adjust Sharpness", 0.0, 2.0, 1.0)
|
| 97 |
+
image = apply_sharpness(image, sharpness_factor)
|
| 98 |
+
|
| 99 |
+
# Blur effect
|
| 100 |
+
blur_radius = st.sidebar.slider("Blur Radius", 0, 10, 0)
|
| 101 |
+
if blur_radius > 0:
|
| 102 |
+
image = apply_blur(image, blur_radius)
|
| 103 |
+
|
| 104 |
+
# Border options
|
| 105 |
+
border_type = st.sidebar.selectbox("Select Border Type", [
|
| 106 |
+
"Solid Color", "Dotted", "Dashed", "Double", "Inset", "Outset", "Rounded Corner",
|
| 107 |
+
"Shadowed", "Polaroid", "Metallic", "Wooden", "Textured", "Neon", "Polka Dots",
|
| 108 |
+
"Stripe", "Sketch", "Lace"])
|
| 109 |
+
|
| 110 |
+
border_color = st.sidebar.color_picker("Pick a Border Color", "#000000")
|
| 111 |
+
border_thickness = st.sidebar.slider("Select Border Thickness", 0, 50, 10)
|
| 112 |
+
|
| 113 |
+
image = add_border(image, border_type, border_color[1:], border_thickness)
|
| 114 |
+
|
| 115 |
+
# Display the modified image
|
| 116 |
+
st.image(image, caption="Modified Image", use_container_width=True)
|
| 117 |
+
|
| 118 |
+
# Download button
|
| 119 |
+
buffered = BytesIO()
|
| 120 |
+
image.save(buffered, format="JPEG")
|
| 121 |
+
buffered.seek(0)
|
| 122 |
+
|
| 123 |
+
st.sidebar.download_button(
|
| 124 |
+
label="Download Edited Image",
|
| 125 |
+
data=buffered,
|
| 126 |
+
file_name="edited_image.jpg",
|
| 127 |
+
mime="image/jpeg"
|
| 128 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
if __name__ == "__main__":
|
| 131 |
main()
|