HedronCreeper commited on
Commit
14b079b
·
verified ·
1 Parent(s): 9532db3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -10
app.py CHANGED
@@ -31,16 +31,20 @@ def home():
31
  img = Image.new("RGB", (256, 256), color=(255, 255, 255))
32
  draw = ImageDraw.Draw(img)
33
 
34
- # Use system font installed in Docker
35
- font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 20)
36
- w, h = draw.textsize(text, font=font)
37
- draw.text(((256-w)/2, (256-h)/2), text, fill="black", font=font)
38
-
39
- # Convert image to base64 to embed in HTML
40
- img_byte_arr = io.BytesIO()
41
- img.save(img_byte_arr, format="PNG")
42
- img_byte_arr.seek(0)
43
- img_b64 = base64.b64encode(img_byte_arr.read()).decode()
 
 
 
 
44
  image_tag = f"<h3>Generated Image:</h3><img src='data:image/png;base64,{img_b64}' alt='Generated Image'>"
45
 
46
  return HTML_TEMPLATE.format(image_tag=image_tag)
 
31
  img = Image.new("RGB", (256, 256), color=(255, 255, 255))
32
  draw = ImageDraw.Draw(img)
33
 
34
+ # Safe font handling
35
+ try:
36
+ font = ImageFont.load_default()
37
+ except:
38
+ font = None
39
+
40
+ # Draw text
41
+ draw.text((10, 120), text, fill="black", font=font)
42
+
43
+ # Convert image to base64
44
+ img_bytes = io.BytesIO()
45
+ img.save(img_bytes, format="PNG")
46
+ img_bytes.seek(0)
47
+ img_b64 = base64.b64encode(img_bytes.read()).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)