#!/usr/bin/env python3 from PIL import Image from PIL import ImageDraw from PIL import ImageFont from io import BytesIO from flask import Flask, send_file app = Flask(__name__) in_file = 'input.jpg' def process(): img = Image.open(in_file) img.thumbnail((500, 500), Image.ANTIALIAS) margin = 30 font_size = 24 font_file = 'Verdana.ttf' w0, h0 = img.size w1, h1 = 2*margin + w0, 2*margin + h0 + 30 out_img = Image.new('RGB', size=(w1, h1), color=(255, 255, 255)) out_img.paste(img, (margin, margin + 30)) font = ImageFont.load_default().font font = ImageFont.truetype(font_file, font_size) draw = ImageDraw.Draw(out_img) draw.text((margin, margin/2), 'me irl:', (0, 0, 0), font=font) return out_img def serve_pil_image(pil_img): img_io = BytesIO() pil_img.save(img_io, 'PNG') img_io.seek(0) return send_file(img_io, mimetype='image/png') @app.route('/image.png') def serve_image(): pil_img = process() return serve_pil_image(pil_img) if __name__ == '__main__': # process() app.run()