Spaces:
Running
Running
File size: 2,326 Bytes
780f02f 1bd2025 780f02f 1bd2025 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 |
import streamlit as st
from PIL import Image, ImageDraw, ImageFont
import os
def add_text_to_image(image, top_text, bottom_text):
draw = ImageDraw.Draw(image)
# Load a font; change the path if not working or to use a custom font.
font_path = "arial.ttf"
font_size = int(image.width / 10) # Dynamic font size based on image width
try:
font = ImageFont.truetype(font_path, font_size)
except:
font = ImageFont.load_default()
# Helper function to calculate text size
def get_text_size(text, font):
bbox = draw.textbbox((0, 0), text, font=font)
return bbox[2] - bbox[0], bbox[3] - bbox[1]
# Top text
text_width, text_height = get_text_size(top_text, font)
top_text_position = ((image.width - text_width) // 2, 10)
draw.text(top_text_position, top_text, font=font, fill="white", stroke_fill="black", stroke_width=2)
# Bottom text
text_width, text_height = get_text_size(bottom_text, font)
bottom_text_position = ((image.width - text_width) // 2, image.height - text_height - 10)
draw.text(bottom_text_position, bottom_text, font=font, fill="white", stroke_fill="black", stroke_width=2)
return image
# Streamlit App Title
st.title("Meme Generator")
# File uploader for the image
uploaded_file = st.file_uploader("Upload an image for your meme:", type=["jpg", "jpeg", "png"])
if uploaded_file:
# Load the uploaded image
image = Image.open(uploaded_file)
# Get user input for captions
top_text = st.text_input("Top Text:", "")
bottom_text = st.text_input("Bottom Text:", "")
# Display the original image
st.image(image, caption="Original Image", use_column_width=True)
if st.button("Generate Meme"):
# Generate meme with captions
meme_image = add_text_to_image(image.copy(), top_text, bottom_text)
# Display the meme
st.image(meme_image, caption="Your Meme", use_column_width=True)
# Provide a download link
output_path = "meme.png"
meme_image.save(output_path)
with open(output_path, "rb") as file:
btn = st.download_button(
label="Download Meme",
data=file,
file_name="meme.png",
mime="image/png"
)
# Footer
st.write("WOW MEMES!!!") |