From 56d70fd9e8507b0c9fd9b9aa52a0537ad5c5b6b2 Mon Sep 17 00:00:00 2001 From: edne Date: Sat, 10 Mar 2018 03:40:05 +0100 Subject: [PATCH] Serve image --- caption.py | 56 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/caption.py b/caption.py index 2297e78..d970834 100755 --- a/caption.py +++ b/caption.py @@ -2,25 +2,53 @@ from PIL import Image from PIL import ImageDraw from PIL import ImageFont +from io import BytesIO -img = Image.open("input.jpg") +from flask import Flask, send_file -img.thumbnail((500, 500), Image.ANTIALIAS) +app = Flask(__name__) -margin = 30 -font_size = 24 -font_file = 'Verdana.ttf' +in_file = 'input.jpg' -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)) +def process(): + img = Image.open(in_file) -font = ImageFont.load_default().font -font = ImageFont.truetype(font_file, font_size) + img.thumbnail((500, 500), Image.ANTIALIAS) -draw = ImageDraw.Draw(out_img) -draw.text((margin, margin/2), 'Antani', (0, 0, 0), font=font) + margin = 30 + font_size = 24 + font_file = 'Verdana.ttf' -out_img.save('output.jpg') + 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()