Spaces:
Sleeping
Sleeping
Edited aoo
Browse files
app.py
CHANGED
@@ -1,38 +1,70 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
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 |
-
st.
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from PIL import Image, ImageDraw, ImageFont
|
3 |
+
import os
|
4 |
|
5 |
+
def add_text_to_image(image, top_text, bottom_text):
|
6 |
+
draw = ImageDraw.Draw(image)
|
7 |
+
|
8 |
+
# Load a font; change the path if not working or to use a custom font.
|
9 |
+
font_path = "arial.ttf"
|
10 |
+
font_size = int(image.width / 10) # Dynamic font size based on image width
|
11 |
+
try:
|
12 |
+
font = ImageFont.truetype(font_path, font_size)
|
13 |
+
except:
|
14 |
+
font = ImageFont.load_default()
|
15 |
+
|
16 |
+
# Helper function to calculate text size
|
17 |
+
def get_text_size(text, font):
|
18 |
+
bbox = draw.textbbox((0, 0), text, font=font)
|
19 |
+
return bbox[2] - bbox[0], bbox[3] - bbox[1]
|
20 |
+
|
21 |
+
# Top text
|
22 |
+
text_width, text_height = get_text_size(top_text, font)
|
23 |
+
top_text_position = ((image.width - text_width) // 2, 10)
|
24 |
+
draw.text(top_text_position, top_text, font=font, fill="white", stroke_fill="black", stroke_width=2)
|
25 |
+
|
26 |
+
# Bottom text
|
27 |
+
text_width, text_height = get_text_size(bottom_text, font)
|
28 |
+
bottom_text_position = ((image.width - text_width) // 2, image.height - text_height - 10)
|
29 |
+
draw.text(bottom_text_position, bottom_text, font=font, fill="white", stroke_fill="black", stroke_width=2)
|
30 |
+
|
31 |
+
return image
|
32 |
+
|
33 |
+
|
34 |
+
# Streamlit App Title
|
35 |
+
st.title("Meme Generator")
|
36 |
+
|
37 |
+
# File uploader for the image
|
38 |
+
uploaded_file = st.file_uploader("Upload an image for your meme:", type=["jpg", "jpeg", "png"])
|
39 |
+
|
40 |
+
if uploaded_file:
|
41 |
+
# Load the uploaded image
|
42 |
+
image = Image.open(uploaded_file)
|
43 |
+
|
44 |
+
# Get user input for captions
|
45 |
+
top_text = st.text_input("Top Text:", "")
|
46 |
+
bottom_text = st.text_input("Bottom Text:", "")
|
47 |
+
|
48 |
+
# Display the original image
|
49 |
+
st.image(image, caption="Original Image", use_column_width=True)
|
50 |
+
|
51 |
+
if st.button("Generate Meme"):
|
52 |
+
# Generate meme with captions
|
53 |
+
meme_image = add_text_to_image(image.copy(), top_text, bottom_text)
|
54 |
+
|
55 |
+
# Display the meme
|
56 |
+
st.image(meme_image, caption="Your Meme", use_column_width=True)
|
57 |
+
|
58 |
+
# Provide a download link
|
59 |
+
output_path = "meme.png"
|
60 |
+
meme_image.save(output_path)
|
61 |
+
with open(output_path, "rb") as file:
|
62 |
+
btn = st.download_button(
|
63 |
+
label="Download Meme",
|
64 |
+
data=file,
|
65 |
+
file_name="meme.png",
|
66 |
+
mime="image/png"
|
67 |
+
)
|
68 |
+
|
69 |
+
# Footer
|
70 |
+
st.write("WOW MEMES!!!")
|