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, ImageFilter
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
| 5 |
import io
|
|
@@ -61,76 +61,31 @@ def apply_edge_detection(image):
|
|
| 61 |
edges = cv2.Canny(img, 100, 200)
|
| 62 |
return Image.fromarray(edges)
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
# Function to convert PIL image to bytes for download
|
| 65 |
def pil_to_bytes(image, format="JPEG"):
|
| 66 |
img_byte_arr = io.BytesIO()
|
| 67 |
image.save(img_byte_arr, format=format)
|
| 68 |
img_byte_arr.seek(0)
|
| 69 |
-
return
|
| 70 |
-
|
| 71 |
-
# Streamlit app
|
| 72 |
-
def main():
|
| 73 |
-
st.title("Advanced Image Editor")
|
| 74 |
-
|
| 75 |
-
# Upload image
|
| 76 |
-
uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 77 |
-
|
| 78 |
-
if uploaded_image is not None:
|
| 79 |
-
# Open the uploaded image using PIL
|
| 80 |
-
image = Image.open(uploaded_image)
|
| 81 |
-
|
| 82 |
-
# Resize the image for better side-by-side display
|
| 83 |
-
original_image_resized = image.resize((400, int(image.height * 400 / image.width)))
|
| 84 |
-
|
| 85 |
-
# Apply filters
|
| 86 |
-
st.sidebar.title("Adjust Filters")
|
| 87 |
-
|
| 88 |
-
filter_option = st.sidebar.selectbox("Choose a filter", ("None", "Grayscale", "Brightness", "Contrast", "Saturation", "Sharpness", "Sepia", "Vignette", "Blur", "Edge Detection"))
|
| 89 |
-
|
| 90 |
-
if filter_option == "Grayscale":
|
| 91 |
-
image = apply_grayscale(image)
|
| 92 |
-
elif filter_option == "Brightness":
|
| 93 |
-
brightness_factor = st.sidebar.slider("Brightness Factor", 0.1, 2.0, 1.0)
|
| 94 |
-
image = adjust_brightness(image, brightness_factor)
|
| 95 |
-
elif filter_option == "Contrast":
|
| 96 |
-
contrast_factor = st.sidebar.slider("Contrast Factor", 0.1, 2.0, 1.0)
|
| 97 |
-
image = adjust_contrast(image, contrast_factor)
|
| 98 |
-
elif filter_option == "Saturation":
|
| 99 |
-
saturation_factor = st.sidebar.slider("Saturation Factor", 0.1, 2.0, 1.0)
|
| 100 |
-
image = adjust_saturation(image, saturation_factor)
|
| 101 |
-
elif filter_option == "Sharpness":
|
| 102 |
-
sharpness_factor = st.sidebar.slider("Sharpness Factor", 0.1, 2.0, 1.0)
|
| 103 |
-
image = apply_sharpness(image, sharpness_factor)
|
| 104 |
-
elif filter_option == "Sepia":
|
| 105 |
-
image = apply_sepia(image)
|
| 106 |
-
elif filter_option == "Vignette":
|
| 107 |
-
image = apply_vignette(image)
|
| 108 |
-
elif filter_option == "Blur":
|
| 109 |
-
blur_factor = st.sidebar.slider("Blur Radius", 0.1, 10.0, 2.0)
|
| 110 |
-
image = apply_blur(image, blur_factor)
|
| 111 |
-
elif filter_option == "Edge Detection":
|
| 112 |
-
image = apply_edge_detection(image)
|
| 113 |
-
|
| 114 |
-
# Resize the filtered image for display
|
| 115 |
-
modified_image_resized = image.resize((400, int(image.height * 400 / image.width)))
|
| 116 |
-
|
| 117 |
-
# Create two columns to display images side by side
|
| 118 |
-
col1, col2 = st.columns(2)
|
| 119 |
-
|
| 120 |
-
# Display the original image in the first column
|
| 121 |
-
with col1:
|
| 122 |
-
st.image(original_image_resized, caption="Original Image", use_container_width=True)
|
| 123 |
-
|
| 124 |
-
# Display the modified image in the second column
|
| 125 |
-
with col2:
|
| 126 |
-
st.image(modified_image_resized, caption="Modified Image", use_container_width=True)
|
| 127 |
-
|
| 128 |
-
# Convert the edited image to bytes for download
|
| 129 |
-
img_bytes = pil_to_bytes(image)
|
| 130 |
-
|
| 131 |
-
# Button to download the edited image below the modified image
|
| 132 |
-
with col2:
|
| 133 |
-
st.download_button("Download Edited Image", img_bytes, "edited_image.jpg", "image/jpeg")
|
| 134 |
-
|
| 135 |
-
if __name__ == "__main__":
|
| 136 |
-
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from PIL import Image, ImageEnhance, ImageFilter, ImageDraw, ImageOps
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
| 5 |
import io
|
|
|
|
| 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 |
+
if frame_style == "Simple":
|
| 73 |
+
# Add simple black border
|
| 74 |
+
frame_width = 30
|
| 75 |
+
return ImageOps.expand(image, border=frame_width, fill="black")
|
| 76 |
+
elif frame_style == "Ornate":
|
| 77 |
+
# Add ornate frame (you can customize with more ornate styles)
|
| 78 |
+
frame_width = 50
|
| 79 |
+
return ImageOps.expand(image, border=frame_width, fill="gold")
|
| 80 |
+
elif frame_style == "Rounded":
|
| 81 |
+
# Add rounded frame (with radius)
|
| 82 |
+
frame_width = 30
|
| 83 |
+
image_with_frame = ImageOps.expand(image, border=frame_width, fill="blue")
|
| 84 |
+
return image_with_frame.convert("RGBA").rotate(0, resample=Image.BICUBIC)
|
| 85 |
+
|
| 86 |
# Function to convert PIL image to bytes for download
|
| 87 |
def pil_to_bytes(image, format="JPEG"):
|
| 88 |
img_byte_arr = io.BytesIO()
|
| 89 |
image.save(img_byte_arr, format=format)
|
| 90 |
img_byte_arr.seek(0)
|
| 91 |
+
return img
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|