Update app.py
Browse files
app.py
CHANGED
@@ -3,15 +3,22 @@ from PIL import Image, ImageDraw, ImageFont
|
|
3 |
import random
|
4 |
import textwrap
|
5 |
|
6 |
-
#
|
7 |
def generate_image(text_description):
|
8 |
-
# Create a blank image with a
|
9 |
-
img = Image.new('RGB', (512, 512), color=(
|
10 |
draw = ImageDraw.Draw(img)
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
# Load a default font (fallback to system font if arial.ttf is unavailable)
|
13 |
try:
|
14 |
-
font = ImageFont.truetype("arial.ttf",
|
15 |
except:
|
16 |
font = ImageFont.load_default()
|
17 |
|
@@ -20,20 +27,21 @@ def generate_image(text_description):
|
|
20 |
wrapped_text = wrapper.fill(text=text_description)
|
21 |
lines = wrapped_text.split('\n')
|
22 |
|
23 |
-
# Draw each line of text
|
24 |
-
y_text =
|
25 |
for line in lines:
|
26 |
-
# Use textbbox to get the bounding box of the text
|
27 |
bbox = draw.textbbox((0, 0), line, font=font)
|
28 |
-
line_width = bbox[2] - bbox[0]
|
29 |
-
line_height = bbox[3] - bbox[1]
|
30 |
draw.text((20, y_text), line, font=font, fill=(255, 255, 255))
|
31 |
-
y_text += line_height + 5
|
32 |
|
33 |
-
#
|
34 |
-
if "lake" in text_description.lower()
|
35 |
-
draw.rectangle([
|
36 |
-
|
|
|
|
|
37 |
|
38 |
return img
|
39 |
|
|
|
3 |
import random
|
4 |
import textwrap
|
5 |
|
6 |
+
# Enhanced function for image generation with better visual representation
|
7 |
def generate_image(text_description):
|
8 |
+
# Create a blank image with a gradient background to simulate a sunset
|
9 |
+
img = Image.new('RGB', (512, 512), color=(0, 0, 0)) # Start with black
|
10 |
draw = ImageDraw.Draw(img)
|
11 |
|
12 |
+
# Create a simple gradient for sunset (orange to purple)
|
13 |
+
for y in range(512):
|
14 |
+
r = int(255 * (1 - y / 512) * 0.8) # Fade red/orange
|
15 |
+
g = int(100 * (1 - y / 512)) # Minimal green
|
16 |
+
b = int(150 + 105 * (y / 512)) # Increase blue/purple
|
17 |
+
draw.line([(0, y), (512, y)], fill=(r, g, b))
|
18 |
+
|
19 |
# Load a default font (fallback to system font if arial.ttf is unavailable)
|
20 |
try:
|
21 |
+
font = ImageFont.truetype("arial.ttf", 25)
|
22 |
except:
|
23 |
font = ImageFont.load_default()
|
24 |
|
|
|
27 |
wrapped_text = wrapper.fill(text=text_description)
|
28 |
lines = wrapped_text.split('\n')
|
29 |
|
30 |
+
# Draw each line of text at the bottom
|
31 |
+
y_text = 400
|
32 |
for line in lines:
|
|
|
33 |
bbox = draw.textbbox((0, 0), line, font=font)
|
34 |
+
line_width = bbox[2] - bbox[0]
|
35 |
+
line_height = bbox[3] - bbox[1]
|
36 |
draw.text((20, y_text), line, font=font, fill=(255, 255, 255))
|
37 |
+
y_text += line_height + 5
|
38 |
|
39 |
+
# Add graphical elements based on description
|
40 |
+
if "lake" in text_description.lower():
|
41 |
+
draw.rectangle([150, 300, 350, 400], fill=(0, 191, 255)) # Blue lake
|
42 |
+
if "mountains" in text_description.lower():
|
43 |
+
draw.polygon([(200, 250), (250, 200), (300, 250)], fill=(255, 255, 255)) # White mountain
|
44 |
+
draw.polygon([(250, 200), (300, 150), (350, 200)], fill=(255, 255, 255)) # Second mountain
|
45 |
|
46 |
return img
|
47 |
|