Spaces:
Sleeping
Sleeping
| 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!!!") |