send an image if no map available

This commit is contained in:
Cyberes 2024-09-04 16:54:21 -06:00
parent 4b4c4d4491
commit 0d53c6a2b5
1 changed files with 12 additions and 1 deletions

View File

@ -2,8 +2,11 @@ import datetime
import io import io
import redis import redis
from PIL import Image, ImageDraw, ImageFont
from flask import Flask, send_file, make_response from flask import Flask, send_file, make_response
NO_MAP_STR = 'NO GLOBAL MAP AVAILABLE'
app = Flask(__name__) app = Flask(__name__)
redis_client = redis.Redis(host='localhost', port=6379) redis_client = redis.Redis(host='localhost', port=6379)
@ -12,7 +15,15 @@ redis_client = redis.Redis(host='localhost', port=6379)
def serve_global_map(): def serve_global_map():
global_map_data = redis_client.get('global_map') global_map_data = redis_client.get('global_map')
if global_map_data is None: 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 = io.BytesIO(global_map_data)
buf.seek(0) buf.seek(0)