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,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()