Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,65 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from PIL import Image,
|
| 3 |
import numpy as np
|
| 4 |
import io
|
| 5 |
|
| 6 |
-
# Function to
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
img = Image.new('RGB', (500, 500), color='white')
|
| 10 |
-
draw = ImageDraw.Draw(img)
|
| 11 |
-
|
| 12 |
-
# Draw shapes
|
| 13 |
-
draw.rectangle([50, 50, 450, 450], outline="black", width=5) # Rectangle
|
| 14 |
-
draw.ellipse([100, 100, 400, 400], outline="blue", width=5) # Circle
|
| 15 |
-
draw.line([0, 0, 500, 500], fill="red", width=3) # Diagonal line
|
| 16 |
-
|
| 17 |
-
# Add text to the image
|
| 18 |
-
font = ImageFont.load_default()
|
| 19 |
-
text = "Custom Image"
|
| 20 |
-
textwidth, textheight = draw.textsize(text, font)
|
| 21 |
-
text_position = ((500 - textwidth) // 2, (500 - textheight) // 2) # Center the text
|
| 22 |
-
draw.text(text_position, text, fill="green", font=font)
|
| 23 |
-
|
| 24 |
-
return img
|
| 25 |
|
| 26 |
# Function to apply brightness adjustment
|
| 27 |
def apply_brightness(image, factor):
|
| 28 |
enhancer = ImageEnhance.Brightness(image)
|
| 29 |
return enhancer.enhance(factor)
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
image = apply_brightness(image, brightness_factor)
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
st.
|
|
|
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
image.save(buffered, format="PNG")
|
| 51 |
-
buffered.seek(0)
|
| 52 |
-
st.download_button("Download Edited Image", buffered, file_name="custom_edited_image.png", mime="image/png")
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from PIL import Image, ImageEnhance, ImageDraw, ImageFont
|
| 3 |
import numpy as np
|
| 4 |
import io
|
| 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 add text to the image
|
| 16 |
+
def add_text(image, text):
|
| 17 |
+
draw = ImageDraw.Draw(image)
|
| 18 |
+
font = ImageFont.load_default()
|
| 19 |
+
width, height = image.size
|
| 20 |
+
text_width, text_height = draw.textsize(text, font=font)
|
| 21 |
+
|
| 22 |
+
# Position text at the bottom center
|
| 23 |
+
x = (width - text_width) / 2
|
| 24 |
+
y = height - text_height - 10
|
| 25 |
+
draw.text((x, y), text, font=font, fill="white")
|
| 26 |
+
return image
|
|
|
|
| 27 |
|
| 28 |
+
# Streamlit UI
|
| 29 |
+
st.title("Image Editor")
|
| 30 |
+
st.write("Upload an image and apply filters or add text to it.")
|
| 31 |
|
| 32 |
+
# Image upload
|
| 33 |
+
uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
if uploaded_image is not None:
|
| 36 |
+
# Open the image using PIL
|
| 37 |
+
image = Image.open(uploaded_image)
|
| 38 |
+
|
| 39 |
+
# Show the original image
|
| 40 |
+
st.image(image, caption="Original Image", use_column_width=True)
|
| 41 |
+
|
| 42 |
+
# Apply filters and editing options
|
| 43 |
+
st.sidebar.title("Edit Options")
|
| 44 |
+
|
| 45 |
+
# Grayscale filter
|
| 46 |
+
if st.sidebar.checkbox("Apply Grayscale"):
|
| 47 |
+
image = apply_grayscale(image)
|
| 48 |
+
|
| 49 |
+
# Brightness adjustment
|
| 50 |
+
brightness_factor = st.sidebar.slider("Adjust Brightness", 0.0, 2.0, 1.0)
|
| 51 |
+
image = apply_brightness(image, brightness_factor)
|
| 52 |
+
|
| 53 |
+
# Add text
|
| 54 |
+
text = st.sidebar.text_input("Add Text to Image")
|
| 55 |
+
if text:
|
| 56 |
+
image = add_text(image, text)
|
| 57 |
+
|
| 58 |
+
# Show edited image
|
| 59 |
+
st.image(image, caption="Edited Image", use_column_width=True)
|
| 60 |
+
|
| 61 |
+
# Download button
|
| 62 |
+
buffered = io.BytesIO()
|
| 63 |
+
image.save(buffered, format="PNG")
|
| 64 |
+
buffered.seek(0)
|
| 65 |
+
st.download_button("Download Edited Image", buffered, file_name="edited_image.png", mime="image/png")
|