Serve image

This commit is contained in:
edne 2018-03-10 03:40:05 +01:00 committed by subnixr
parent fb8b6fe9b8
commit 56d70fd9e8

View File

@ -2,8 +2,17 @@
from PIL import Image from PIL import Image
from PIL import ImageDraw from PIL import ImageDraw
from PIL import ImageFont from PIL import ImageFont
from io import BytesIO
img = Image.open("input.jpg") 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) img.thumbnail((500, 500), Image.ANTIALIAS)
@ -21,6 +30,25 @@ font = ImageFont.load_default().font
font = ImageFont.truetype(font_file, font_size) font = ImageFont.truetype(font_file, font_size)
draw = ImageDraw.Draw(out_img) draw = ImageDraw.Draw(out_img)
draw.text((margin, margin/2), 'Antani', (0, 0, 0), font=font) draw.text((margin, margin/2), 'me irl:', (0, 0, 0), font=font)
out_img.save('output.jpg') 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()