image_creator / app.py
sikandarciv101's picture
Update app.py
bf149dd verified
import streamlit as st
from PIL import Image, ImageDraw, ImageFont, ImageEnhance
import numpy as np
import io
# Function to create an image with shapes and text
def create_image():
# Create a blank white image (width=500px, height=500px)
img = Image.new('RGB', (500, 500), color='white')
draw = ImageDraw.Draw(img)
# Draw shapes
draw.rectangle([50, 50, 450, 450], outline="black", width=5) # Rectangle
draw.ellipse([100, 100, 400, 400], outline="blue", width=5) # Circle
draw.line([0, 0, 500, 500], fill="red", width=3) # Diagonal line
# Load a font (use the system's default font or provide your own .ttf font file)
try:
font = ImageFont.truetype("DejaVuSans-Bold.ttf", size=30) # Use a TrueType font
except IOError:
font = ImageFont.load_default() # Fallback to default font if the TrueType font is not available
# Add text to the image
text = "Custom Image"
# Use textbbox instead of textsize
text_bbox = draw.textbbox((0, 0), text, font=font)
textwidth = text_bbox[2] - text_bbox[0] # Width of the text
textheight = text_bbox[3] - text_bbox[1] # Height of the text
text_position = ((500 - textwidth) // 2, (500 - textheight) // 2) # Center the text
draw.text(text_position, text, fill="green", font=font)
return img
# Function to apply brightness adjustment
def apply_brightness(image, factor):
enhancer = ImageEnhance.Brightness(image)
return enhancer.enhance(factor)
# Streamlit UI
st.title("Create and Edit Custom Images")
st.write("Create a custom image with shapes and text, and apply filters to it.")
# Create a custom image
image = create_image()
# Show the original image
st.image(image, caption="Custom Created Image", use_column_width=True)
# Apply brightness adjustment
brightness_factor = st.slider("Adjust Brightness", 0.0, 2.0, 1.0)
image = apply_brightness(image, brightness_factor)
# Show edited image
st.image(image, caption="Edited Custom Image", use_column_width=True)
# Download button for the edited image
buffered = io.BytesIO()
image.save(buffered, format="PNG")
buffered.seek(0)
st.download_button("Download Edited Image", buffered, file_name="custom_edited_image.png", mime="image/png")