Created
February 12, 2020 05:39
-
-
Save sempr/4f80d639804b03950dd5520891adbea7 to your computer and use it in GitHub Desktop.
Flask&Pillow&Docker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, make_response | |
from PIL import Image | |
from io import BytesIO | |
app = Flask(__name__) | |
@app.route('/') | |
def index(): | |
return 'Hello, World!' | |
@app.route("/image") | |
@app.route('/image/<int:width>/<int:height>') | |
@app.route('/image/<int:width>/<int:height>/<string:color>') | |
@app.route('/image/<int:width>/<int:height>/<string:color>.<string:format>') | |
def image(width=100, height=100, color="RED", format="png"): | |
im = Image.new("RGBA", (width, height), color) | |
if format.upper() == "JPG": | |
format = "JPEG" | |
f = BytesIO() | |
print(Image.SAVE.keys()) | |
im.save(f, format=format) | |
resp = make_response(f.getvalue()) | |
resp.headers["content-type"] = f"image/{format}" | |
return resp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM python:3.8.1-slim | |
ADD ./requirements.txt /tmp/requirements.txt | |
RUN set -xe \ | |
&& pip install -i https://mirrors.aliyun.com/pypi/simple --no-cache -r /tmp/requirements.txt | |
ADD app.py /code/app.py | |
WORKDIR /code | |
CMD ["gunicorn", "-w4", "-b0.0.0.0:8000", "app:app"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Click==7.0 | |
Flask==1.1.1 | |
gunicorn==20.0.4 | |
itsdangerous==1.1.0 | |
Jinja2==2.11.1 | |
MarkupSafe==1.1.1 | |
Pillow==7.0.0 | |
Werkzeug==1.0.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment