Serve image
This commit is contained in:
parent
fb8b6fe9b8
commit
56d70fd9e8
56
caption.py
56
caption.py
|
@ -2,25 +2,53 @@
|
||||||
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
|
||||||
|
|
||||||
img.thumbnail((500, 500), Image.ANTIALIAS)
|
app = Flask(__name__)
|
||||||
|
|
||||||
margin = 30
|
in_file = 'input.jpg'
|
||||||
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))
|
def process():
|
||||||
out_img.paste(img, (margin, margin + 30))
|
img = Image.open(in_file)
|
||||||
|
|
||||||
font = ImageFont.load_default().font
|
img.thumbnail((500, 500), Image.ANTIALIAS)
|
||||||
font = ImageFont.truetype(font_file, font_size)
|
|
||||||
|
|
||||||
draw = ImageDraw.Draw(out_img)
|
margin = 30
|
||||||
draw.text((margin, margin/2), 'Antani', (0, 0, 0), font=font)
|
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()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user