47 lines
1.3 KiB
Python
47 lines
1.3 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://localhost:8080/api/tag', data=payload, 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
|
|
|
|
if __name__ == '__main__':
|
|
app.run('0.0.0.0', port=80)
|