Created
March 25, 2023 05:31
-
-
Save ryo-rm/db6dce4c691508dc3cba28b4603e3eb2 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
import sys | |
import magic | |
from PIL import Image | |
from inky.inky_uc8159 import Inky | |
from flask import Flask, request, Response | |
inky = Inky() | |
saturation = 0.5 | |
app = Flask(__name__) | |
def validate_image(buffer): | |
mime_type = magic.from_buffer(buffer, mime=True) | |
if mime_type == 'image/png' or mime_type == 'image/jpeg' or mime_type == 'image/webp' or mime_type == 'image/gif': | |
return True | |
elif mime_type == 'image/heic': | |
return False | |
else: | |
return False | |
def convert_to_epaper(input_image, epaper_width=640, epaper_height=400): | |
# 画像が640x400ピクセルの場合はそのまま出力 | |
if input_image.size == (epaper_width, epaper_height): | |
return input_image | |
# アスペクト比を保ったままリサイズ | |
width, height = input_image.size | |
aspect_ratio = width / height | |
target_ratio = epaper_width / epaper_height | |
if aspect_ratio > target_ratio: | |
# 横長の場合 | |
new_width = epaper_width | |
new_height = int(new_width / aspect_ratio) | |
else: | |
# 縦長の場合 | |
new_height = epaper_height | |
new_width = int(new_height * aspect_ratio) | |
input_image = input_image.resize((new_width, new_height), resample=Image.LANCZOS) | |
# 透過PNGの場合、透過部分を白で塗りつぶす | |
if input_image.mode == 'RGBA': | |
background = Image.new('RGBA', input_image.size, (255, 255, 255, 255)) | |
background.paste(input_image, mask=input_image) | |
input_image = background.convert('RGB') | |
# 足りない領域を白で塗りつぶす | |
padded_image = Image.new("RGB", (epaper_width, epaper_height), color=(255, 255, 255)) | |
left = (epaper_width - new_width) // 2 | |
upper = (epaper_height - new_height) // 2 | |
padded_image.paste(input_image, (left, upper)) | |
return padded_image | |
# @app.before_request | |
# def log_request_info(): | |
# app.logger.debug('Headers: %s', request.headers) | |
# app.logger.debug('Body: %s', request.get_data()) | |
@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 validate_image(file.read()): | |
try: | |
image = Image.open(file) | |
converted = convert_to_epaper(image) | |
inky.set_image(converted, saturation=saturation) | |
inky.show() | |
ret = 'upload success' | |
code = 200 | |
except ValueError as e: | |
print(e) | |
ret = str(e) | |
code = 400 | |
except Exception as e: | |
print(e) | |
code = 500 | |
ret = str(e) | |
return Response(response=ret, status=code) | |
# app.run(debug=True, host='0.0.0.0', port=7777) | |
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