58 lines
1.5 KiB
Python
58 lines
1.5 KiB
Python
from flask import Flask, request, jsonify
|
|
from flask_cors import CORS
|
|
from PIL import Image
|
|
import requests
|
|
import io
|
|
|
|
app = Flask(__name__)
|
|
CORS(app)
|
|
|
|
@app.route('/')
|
|
def root():
|
|
return 'ok'
|
|
|
|
@app.route('/api/bitmap', methods=['POST'])
|
|
def bitmap():
|
|
|
|
if 'file' not in request.files:
|
|
return jsonify({"error": "no file"}), 400
|
|
|
|
file = request.files['file']
|
|
|
|
if file.filename == '':
|
|
return jsonify({"error": "no file"}), 400
|
|
|
|
if not file.filename.lower().endswith('.png'):
|
|
return jsonify({"error": "invalid file format"}), 400
|
|
|
|
try:
|
|
image_stream = io.BytesIO(file.read())
|
|
img = Image.open(image_stream)
|
|
grayscale_img = img.convert('L')
|
|
bitmap_data = list(grayscale_img.getdata())
|
|
|
|
payload = 'P5\n296 152\n255\n'
|
|
payload += ''.join([chr(i) for i in bitmap_data])
|
|
print(payload)
|
|
req = requests.put('http://host.docker.internal:8080/api/tag', data=chunk_gen(bitmap_data), headers={
|
|
'Content-Type': 'image/x-portable-greymap'
|
|
})
|
|
return jsonify({'payload': payload, 'code': req.status_code})
|
|
# return jsonify({'code': req.status_code})
|
|
|
|
except Exception as e:
|
|
return jsonify({"error": e}), 500
|
|
|
|
|
|
def chunk_gen(bitmap_data):
|
|
header = f'P5\n296 152\n255\n'
|
|
yield header.encode('latin-1')
|
|
|
|
chunk_size = 1024
|
|
for i in range(0, len(bitmap_data), chunk_size):
|
|
chunk = bitmap_data[i:i + chunk_size]
|
|
yield bytes(chunk)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run('0.0.0.0', port=80)
|