Skip to content

Instantly share code, notes, and snippets.

@Akkiesoft
Created October 29, 2021 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Akkiesoft/72a75371e675620a9fc9c41c0e0a3dcc to your computer and use it in GitHub Desktop.
Save Akkiesoft/72a75371e675620a9fc9c41c0e0a3dcc to your computer and use it in GitHub Desktop.
HTTP POSTリクエストでPimoroni Inky Impressionの画像を書き換えるやつ
#!/usr/bin/env python3
import sys
from PIL import Image
from inky.inky_uc8159 import Inky
from flask import Flask, request, Response
inky = Inky()
saturation = 0.5
ALLOWED_EXTENSIONS = {'jpg', 'png'}
app = Flask(__name__)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/upload', methods=['POST'])
def upload_file():
code = 400
# check if the post request has the file part
if 'file' not in request.files:
ret = 'No file part'
file = request.files['file']
if file and allowed_file(file.filename):
try:
image = Image.open(file)
inky.set_image(image, saturation=saturation)
inky.show()
ret = 'upload success'
code = 200
except ValueError as e:
ret = str(e)
else:
ret = 'Invalid file type'
return Response(response=ret, status=code)
app.run(debug=False, host='0.0.0.0', port=7777)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment