Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,98 +1,81 @@
|
|
| 1 |
-
### app.py ###
|
| 2 |
import streamlit as st
|
| 3 |
-
from PIL import Image, ImageEnhance, ImageFilter
|
| 4 |
import numpy as np
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
elif
|
| 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 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
# Apply filters and adjustments
|
| 84 |
-
edited_image = apply_filters(cropped_image, filter_option, brightness, contrast, saturation, tint)
|
| 85 |
-
st.image(edited_image, caption="Edited Image", use_column_width=True)
|
| 86 |
-
|
| 87 |
-
if st.sidebar.button("Download Edited Image"):
|
| 88 |
-
edited_image.save("edited_image.png")
|
| 89 |
-
with open("edited_image.png", "rb") as file:
|
| 90 |
-
btn = st.download_button(
|
| 91 |
-
label="Download",
|
| 92 |
-
data=file,
|
| 93 |
-
file_name="edited_image.png",
|
| 94 |
-
mime="image/png"
|
| 95 |
-
)
|
| 96 |
-
|
| 97 |
-
if __name__ == "__main__":
|
| 98 |
-
main()
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from PIL import Image, ImageEnhance, ImageFilter, ImageDraw, ImageFont
|
| 3 |
import numpy as np
|
| 4 |
+
import io
|
| 5 |
+
|
| 6 |
+
# Function to apply filters
|
| 7 |
+
def apply_filter(img, filter_name, value):
|
| 8 |
+
if filter_name == 'Grayscale':
|
| 9 |
+
return img.convert("L")
|
| 10 |
+
elif filter_name == 'Blur':
|
| 11 |
+
return img.filter(ImageFilter.GaussianBlur(radius=value))
|
| 12 |
+
elif filter_name == 'Magic Eraser':
|
| 13 |
+
img = np.array(img)
|
| 14 |
+
img[img == 255] = 0 # Magic Eraser: remove white pixels
|
| 15 |
+
return Image.fromarray(img)
|
| 16 |
+
elif filter_name == 'Sharpness':
|
| 17 |
+
enhancer = ImageEnhance.Sharpness(img)
|
| 18 |
+
return enhancer.enhance(value)
|
| 19 |
+
elif filter_name == 'Contrast':
|
| 20 |
+
enhancer = ImageEnhance.Contrast(img)
|
| 21 |
+
return enhancer.enhance(value)
|
| 22 |
+
elif filter_name == 'Saturation':
|
| 23 |
+
enhancer = ImageEnhance.Color(img)
|
| 24 |
+
return enhancer.enhance(value)
|
| 25 |
+
elif filter_name == 'Brightness':
|
| 26 |
+
enhancer = ImageEnhance.Brightness(img)
|
| 27 |
+
return enhancer.enhance(value)
|
| 28 |
+
elif filter_name == 'Tint':
|
| 29 |
+
img = np.array(img)
|
| 30 |
+
img = np.clip(img + (value * 50), 0, 255)
|
| 31 |
+
return Image.fromarray(img)
|
| 32 |
+
else:
|
| 33 |
+
return img
|
| 34 |
+
|
| 35 |
+
def crop_image(img, left, top, right, bottom):
|
| 36 |
+
return img.crop((left, top, right, bottom))
|
| 37 |
+
|
| 38 |
+
# Streamlit UI components
|
| 39 |
+
st.title('Image Editor')
|
| 40 |
+
|
| 41 |
+
# Upload Image
|
| 42 |
+
uploaded_file = st.file_uploader("Upload Image", type=["jpg", "png", "jpeg"])
|
| 43 |
+
|
| 44 |
+
if uploaded_file is not None:
|
| 45 |
+
# Open the uploaded image
|
| 46 |
+
img = Image.open(uploaded_file)
|
| 47 |
+
|
| 48 |
+
# Display the original image
|
| 49 |
+
st.image(img, caption="Uploaded Image", use_column_width=True)
|
| 50 |
+
|
| 51 |
+
# Filter options
|
| 52 |
+
filter_name = st.selectbox("Choose a filter",
|
| 53 |
+
["None", "Grayscale", "Blur", "Magic Eraser", "Sharpness", "Contrast", "Saturation", "Brightness", "Tint"])
|
| 54 |
+
|
| 55 |
+
if filter_name in ["Blur", "Sharpness", "Contrast", "Saturation", "Brightness"]:
|
| 56 |
+
value = st.slider(f"Adjust {filter_name}", 0.0, 2.0, 1.0)
|
| 57 |
+
elif filter_name == "Tint":
|
| 58 |
+
value = st.slider("Adjust Tint", -1.0, 1.0, 0.0)
|
| 59 |
+
else:
|
| 60 |
+
value = 0
|
| 61 |
+
|
| 62 |
+
if filter_name != "None":
|
| 63 |
+
img = apply_filter(img, filter_name, value)
|
| 64 |
+
|
| 65 |
+
# Cropping
|
| 66 |
+
st.write("Crop Image")
|
| 67 |
+
left = st.slider("Left", 0, img.width, 0)
|
| 68 |
+
top = st.slider("Top", 0, img.height, 0)
|
| 69 |
+
right = st.slider("Right", 0, img.width, img.width)
|
| 70 |
+
bottom = st.slider("Bottom", 0, img.height, img.height)
|
| 71 |
+
|
| 72 |
+
img = crop_image(img, left, top, right, bottom)
|
| 73 |
+
|
| 74 |
+
# Display updated image
|
| 75 |
+
st.image(img, caption="Edited Image", use_column_width=True)
|
| 76 |
+
|
| 77 |
+
# Download the edited image
|
| 78 |
+
buffer = io.BytesIO()
|
| 79 |
+
img.save(buffer, format="PNG")
|
| 80 |
+
buffer.seek(0)
|
| 81 |
+
st.download_button(label="Download Image", data=buffer, file_name="edited_image.png", mime="image/png")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|