Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,23 +2,40 @@ import streamlit as st
|
|
| 2 |
from PIL import Image, ImageEnhance, ImageFilter
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
|
|
|
| 5 |
|
| 6 |
# Title
|
| 7 |
-
st.title("
|
| 8 |
|
| 9 |
-
# Upload image
|
| 10 |
-
|
|
|
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
if uploaded_file is not None:
|
| 13 |
-
# Open image using PIL
|
| 14 |
image = Image.open(uploaded_file)
|
| 15 |
-
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
st.sidebar.
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
# Apply
|
| 21 |
-
filter_option = st.sidebar.selectbox("Choose a filter", ["None", "Vivid", "Warm", "Cool", "Black & White", "Sepia"])
|
| 22 |
if filter_option == "Vivid":
|
| 23 |
image = image.convert("RGB")
|
| 24 |
np_img = np.array(image)
|
|
@@ -41,56 +58,51 @@ if uploaded_file is not None:
|
|
| 41 |
sepia_filter = np.array([[0.393, 0.769, 0.189], [0.349, 0.686, 0.168], [0.272, 0.534, 0.131]])
|
| 42 |
image = Image.fromarray(np.dot(img_array[...,:3], sepia_filter.T).clip(0, 255).astype(np.uint8))
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
if
|
| 46 |
-
brightness = st.sidebar.slider("Brightness", 0.0, 2.0, 1.0)
|
| 47 |
enhancer = ImageEnhance.Brightness(image)
|
| 48 |
image = enhancer.enhance(brightness)
|
| 49 |
|
| 50 |
-
if
|
| 51 |
-
contrast = st.sidebar.slider("Contrast", 0.0, 2.0, 1.0)
|
| 52 |
enhancer = ImageEnhance.Contrast(image)
|
| 53 |
image = enhancer.enhance(contrast)
|
| 54 |
|
| 55 |
-
if
|
| 56 |
-
saturation = st.sidebar.slider("Saturation", 0.0, 2.0, 1.0)
|
| 57 |
enhancer = ImageEnhance.Color(image)
|
| 58 |
image = enhancer.enhance(saturation)
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
|
|
|
|
| 62 |
left = st.sidebar.slider("Left", 0, image.width, 0)
|
| 63 |
top = st.sidebar.slider("Top", 0, image.height, 0)
|
| 64 |
right = st.sidebar.slider("Right", left, image.width, image.width)
|
| 65 |
bottom = st.sidebar.slider("Bottom", top, image.height, image.height)
|
| 66 |
image = image.crop((left, top, right, bottom))
|
| 67 |
|
| 68 |
-
|
| 69 |
-
if
|
| 70 |
width = st.sidebar.slider("Width", 50, 1000, image.width)
|
| 71 |
height = st.sidebar.slider("Height", 50, 1000, image.height)
|
| 72 |
image = image.resize((width, height))
|
| 73 |
|
| 74 |
-
# Sharpen Image (Manual control over sharpening)
|
| 75 |
-
if st.sidebar.checkbox("Sharpen Image"):
|
| 76 |
-
image = image.filter(ImageFilter.SHARPEN)
|
| 77 |
-
|
| 78 |
# Rotate/Flip Image
|
| 79 |
-
|
| 80 |
-
|
|
|
|
| 81 |
image = image.rotate(angle)
|
| 82 |
|
| 83 |
-
|
|
|
|
| 84 |
flip_option = st.sidebar.radio("Flip Option", ["Horizontal", "Vertical"])
|
| 85 |
if flip_option == "Horizontal":
|
| 86 |
image = image.transpose(Image.FLIP_LEFT_RIGHT)
|
| 87 |
else:
|
| 88 |
image = image.transpose(Image.FLIP_TOP_BOTTOM)
|
| 89 |
|
| 90 |
-
#
|
| 91 |
st.image(image, caption="Edited Image", use_column_width=True)
|
| 92 |
|
| 93 |
-
#
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
st.success("Image saved as 'edited_image.jpg'")
|
|
|
|
| 2 |
from PIL import Image, ImageEnhance, ImageFilter
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
| 5 |
+
import io
|
| 6 |
|
| 7 |
# Title
|
| 8 |
+
st.title("Interactive Photo Editor - iPhone-like Features")
|
| 9 |
|
| 10 |
+
# Sidebar: Upload image and edit controls
|
| 11 |
+
st.sidebar.title("Edit Controls")
|
| 12 |
+
uploaded_file = st.sidebar.file_uploader("Upload an Image", type=["png", "jpg", "jpeg"])
|
| 13 |
|
| 14 |
+
# Function to save image as a downloadable link
|
| 15 |
+
def get_image_download_link(img: Image, filename: str):
|
| 16 |
+
img.save(filename)
|
| 17 |
+
with open(filename, "rb") as f:
|
| 18 |
+
img_bytes = f.read()
|
| 19 |
+
download_link = st.sidebar.download_button(
|
| 20 |
+
label="Download Edited Image",
|
| 21 |
+
data=img_bytes,
|
| 22 |
+
file_name=filename,
|
| 23 |
+
mime="image/jpeg"
|
| 24 |
+
)
|
| 25 |
+
return download_link
|
| 26 |
+
|
| 27 |
+
# Initialize image if uploaded
|
| 28 |
if uploaded_file is not None:
|
|
|
|
| 29 |
image = Image.open(uploaded_file)
|
| 30 |
+
st.sidebar.image(image, caption="Uploaded Image", use_column_width=True)
|
| 31 |
|
| 32 |
+
# Image Edit Options
|
| 33 |
+
filter_option = st.sidebar.selectbox("Choose a Filter", ["None", "Vivid", "Warm", "Cool", "Black & White", "Sepia"])
|
| 34 |
+
brightness = st.sidebar.slider("Adjust Brightness", 0.0, 2.0, 1.0)
|
| 35 |
+
contrast = st.sidebar.slider("Adjust Contrast", 0.0, 2.0, 1.0)
|
| 36 |
+
saturation = st.sidebar.slider("Adjust Saturation", 0.0, 2.0, 1.0)
|
| 37 |
|
| 38 |
+
# Apply chosen filters
|
|
|
|
| 39 |
if filter_option == "Vivid":
|
| 40 |
image = image.convert("RGB")
|
| 41 |
np_img = np.array(image)
|
|
|
|
| 58 |
sepia_filter = np.array([[0.393, 0.769, 0.189], [0.349, 0.686, 0.168], [0.272, 0.534, 0.131]])
|
| 59 |
image = Image.fromarray(np.dot(img_array[...,:3], sepia_filter.T).clip(0, 255).astype(np.uint8))
|
| 60 |
|
| 61 |
+
# Apply manual adjustments
|
| 62 |
+
if brightness != 1.0:
|
|
|
|
| 63 |
enhancer = ImageEnhance.Brightness(image)
|
| 64 |
image = enhancer.enhance(brightness)
|
| 65 |
|
| 66 |
+
if contrast != 1.0:
|
|
|
|
| 67 |
enhancer = ImageEnhance.Contrast(image)
|
| 68 |
image = enhancer.enhance(contrast)
|
| 69 |
|
| 70 |
+
if saturation != 1.0:
|
|
|
|
| 71 |
enhancer = ImageEnhance.Color(image)
|
| 72 |
image = enhancer.enhance(saturation)
|
| 73 |
|
| 74 |
+
# Sidebar for cropping, resizing, rotating
|
| 75 |
+
crop_enabled = st.sidebar.checkbox("Enable Crop", False)
|
| 76 |
+
if crop_enabled:
|
| 77 |
left = st.sidebar.slider("Left", 0, image.width, 0)
|
| 78 |
top = st.sidebar.slider("Top", 0, image.height, 0)
|
| 79 |
right = st.sidebar.slider("Right", left, image.width, image.width)
|
| 80 |
bottom = st.sidebar.slider("Bottom", top, image.height, image.height)
|
| 81 |
image = image.crop((left, top, right, bottom))
|
| 82 |
|
| 83 |
+
resize_enabled = st.sidebar.checkbox("Enable Resize", False)
|
| 84 |
+
if resize_enabled:
|
| 85 |
width = st.sidebar.slider("Width", 50, 1000, image.width)
|
| 86 |
height = st.sidebar.slider("Height", 50, 1000, image.height)
|
| 87 |
image = image.resize((width, height))
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
# Rotate/Flip Image
|
| 90 |
+
rotate_enabled = st.sidebar.checkbox("Enable Rotation", False)
|
| 91 |
+
if rotate_enabled:
|
| 92 |
+
angle = st.sidebar.slider("Rotation Angle", 0, 360, 0)
|
| 93 |
image = image.rotate(angle)
|
| 94 |
|
| 95 |
+
flip_enabled = st.sidebar.checkbox("Flip Image", False)
|
| 96 |
+
if flip_enabled:
|
| 97 |
flip_option = st.sidebar.radio("Flip Option", ["Horizontal", "Vertical"])
|
| 98 |
if flip_option == "Horizontal":
|
| 99 |
image = image.transpose(Image.FLIP_LEFT_RIGHT)
|
| 100 |
else:
|
| 101 |
image = image.transpose(Image.FLIP_TOP_BOTTOM)
|
| 102 |
|
| 103 |
+
# Image preview in the main area
|
| 104 |
st.image(image, caption="Edited Image", use_column_width=True)
|
| 105 |
|
| 106 |
+
# Provide download option
|
| 107 |
+
get_image_download_link(image, "edited_image.jpg")
|
| 108 |
+
|
|
|