Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,24 +27,24 @@ def home():
|
|
| 27 |
if request.method == "POST":
|
| 28 |
text = request.form.get("prompt", "Hello!")
|
| 29 |
|
| 30 |
-
# Create image
|
| 31 |
img = Image.new("RGB", (256, 256), color=(255, 255, 255))
|
| 32 |
draw = ImageDraw.Draw(img)
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
font = ImageFont.load_default()
|
| 37 |
-
except:
|
| 38 |
-
font = None
|
| 39 |
|
| 40 |
-
# Draw text
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
-
#
|
| 44 |
img_bytes = io.BytesIO()
|
| 45 |
img.save(img_bytes, format="PNG")
|
| 46 |
img_bytes.seek(0)
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
image_tag = f"<h3>Generated Image:</h3><img src='data:image/png;base64,{img_b64}' alt='Generated Image'>"
|
| 49 |
|
| 50 |
return HTML_TEMPLATE.format(image_tag=image_tag)
|
|
|
|
| 27 |
if request.method == "POST":
|
| 28 |
text = request.form.get("prompt", "Hello!")
|
| 29 |
|
| 30 |
+
# Create blank image
|
| 31 |
img = Image.new("RGB", (256, 256), color=(255, 255, 255))
|
| 32 |
draw = ImageDraw.Draw(img)
|
| 33 |
|
| 34 |
+
# Use Pillow default font (always works in slim Python)
|
| 35 |
+
font = ImageFont.load_default()
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
# Draw text centered
|
| 38 |
+
w, h = draw.textsize(text, font=font)
|
| 39 |
+
draw.text(((256 - w) / 2, (256 - h) / 2), text, fill="black", font=font)
|
| 40 |
|
| 41 |
+
# Save image to bytes
|
| 42 |
img_bytes = io.BytesIO()
|
| 43 |
img.save(img_bytes, format="PNG")
|
| 44 |
img_bytes.seek(0)
|
| 45 |
+
|
| 46 |
+
# Encode to base64 for embedding
|
| 47 |
+
img_b64 = base64.b64encode(img_bytes.getvalue()).decode()
|
| 48 |
image_tag = f"<h3>Generated Image:</h3><img src='data:image/png;base64,{img_b64}' alt='Generated Image'>"
|
| 49 |
|
| 50 |
return HTML_TEMPLATE.format(image_tag=image_tag)
|