umarabbas890 commited on
Commit
c3ca057
·
verified ·
1 Parent(s): d8c6131

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image, ImageDraw, ImageFont
3
+ import io
4
+
5
+ # Set up the app
6
+ def main():
7
+ st.title("Text to Image Converter")
8
+
9
+ st.write("Enter text to generate an image containing the text.")
10
+
11
+ # User input
12
+ user_text = st.text_input("Enter your text:")
13
+
14
+ if st.button("Generate Image"):
15
+ if user_text.strip():
16
+ # Generate image with text
17
+ img = create_text_image(user_text)
18
+ st.image(img, caption="Generated Image", use_column_width=True)
19
+ else:
20
+ st.warning("Please enter some text before generating an image.")
21
+
22
+ def create_text_image(text):
23
+ # Create a blank image with white background
24
+ width, height = 800, 400
25
+ img = Image.new("RGB", (width, height), color="white")
26
+ draw = ImageDraw.Draw(img)
27
+
28
+ # Load a font (default font if custom fonts aren't available)
29
+ try:
30
+ font = ImageFont.truetype("arial.ttf", size=36)
31
+ except IOError:
32
+ font = ImageFont.load_default()
33
+
34
+ # Calculate text position to center it
35
+ text_width, text_height = draw.textsize(text, font=font)
36
+ text_x = (width - text_width) // 2
37
+ text_y = (height - text_height) // 2
38
+
39
+ # Draw text on the image
40
+ draw.text((text_x, text_y), text, fill="black", font=font)
41
+
42
+ # Convert to BytesIO for display
43
+ img_byte_arr = io.BytesIO()
44
+ img.save(img_byte_arr, format="PNG")
45
+ img_byte_arr.seek(0)
46
+ return img_byte_arr
47
+
48
+ # Run the app
49
+ if __name__ == "__main__":
50
+ main()