From 0d53c6a2b540d635d07f388938b73c659529afaf Mon Sep 17 00:00:00 2001 From: Cyberes Date: Wed, 4 Sep 2024 16:54:21 -0600 Subject: [PATCH] send an image if no map available --- feeder/server.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/feeder/server.py b/feeder/server.py index 0c8cdd4..f6703eb 100644 --- a/feeder/server.py +++ b/feeder/server.py @@ -2,8 +2,11 @@ import datetime import io import redis +from PIL import Image, ImageDraw, ImageFont from flask import Flask, send_file, make_response +NO_MAP_STR = 'NO GLOBAL MAP AVAILABLE' + app = Flask(__name__) redis_client = redis.Redis(host='localhost', port=6379) @@ -12,7 +15,15 @@ redis_client = redis.Redis(host='localhost', port=6379) def serve_global_map(): global_map_data = redis_client.get('global_map') if global_map_data is None: - return "No global map available", 400 + img = Image.new('RGB', (500, 300), color=(255, 255, 255)) + d = ImageDraw.Draw(img) + fnt = ImageFont.load_default(size=30) + w, h = fnt.getbbox(NO_MAP_STR)[2:4] + d.text(((500 - w) / 2, (300 - h) / 2), NO_MAP_STR, font=fnt, fill=(0, 0, 0)) + buf = io.BytesIO() + img.save(buf, format='PNG') + buf.seek(0) + return send_file(buf, mimetype='image/png') buf = io.BytesIO(global_map_data) buf.seek(0)