add new format
This commit is contained in:
parent
6c50042361
commit
07256f5dc3
4 changed files with 7 additions and 96 deletions
|
|
@ -31,10 +31,13 @@ def bitmap():
|
||||||
grayscale_img = img.convert('L')
|
grayscale_img = img.convert('L')
|
||||||
bitmap_data = list(grayscale_img.getdata())
|
bitmap_data = list(grayscale_img.getdata())
|
||||||
|
|
||||||
payload = ''.join([chr(i) for i in bitmap_data])
|
payload = 'P5\n296 152\n255\n'
|
||||||
|
payload += ''.join([chr(i) for i in bitmap_data])
|
||||||
print(payload)
|
print(payload)
|
||||||
# req = requests.post('http://10.141.142.75/api/tag', data=payload)
|
req = requests.put('http://localhost:8080/api/tag', data=payload, headers={
|
||||||
return jsonify({'payload': payload})
|
'Content-Type': 'image/x-portable-greymap'
|
||||||
|
})
|
||||||
|
return jsonify({'payload': payload, 'code': req.status_code})
|
||||||
# return jsonify({'code': req.status_code})
|
# return jsonify({'code': req.status_code})
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
|
||||||
55
trash/app.py
55
trash/app.py
|
|
@ -1,55 +0,0 @@
|
||||||
from flask import Flask, request, jsonify
|
|
||||||
from flask_cors import CORS
|
|
||||||
from PIL import Image
|
|
||||||
import io
|
|
||||||
from tcp import send_request
|
|
||||||
|
|
||||||
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 = ''.join([chr(i) for i in bitmap_data])
|
|
||||||
print(payload)
|
|
||||||
|
|
||||||
chunk_size = 2048
|
|
||||||
for i in range(0, len(payload), chunk_size):
|
|
||||||
data = {
|
|
||||||
"command": "update_image",
|
|
||||||
"index": i,
|
|
||||||
"data": bytes(payload[i:i+chunk_size])
|
|
||||||
}
|
|
||||||
send_request(data)
|
|
||||||
send_request({"command":"push_image"})
|
|
||||||
|
|
||||||
# req = requests.post('http://10.141.142.75/api/tag', data=payload)
|
|
||||||
return jsonify({'payload': payload})
|
|
||||||
# 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)
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
FROM python:3.9
|
|
||||||
|
|
||||||
WORKDIR /app
|
|
||||||
|
|
||||||
COPY . .
|
|
||||||
|
|
||||||
RUN pip3 install pillow flask flask-cors requests
|
|
||||||
|
|
||||||
CMD ["python3", "app.py"]
|
|
||||||
28
trash/tcp.py
28
trash/tcp.py
|
|
@ -1,28 +0,0 @@
|
||||||
from typing import Dict, Any
|
|
||||||
import socket
|
|
||||||
|
|
||||||
import msgpack
|
|
||||||
|
|
||||||
SERVER_ADDRESS = ('172.16.0.249', 8888)
|
|
||||||
|
|
||||||
def send_request(data:Dict[str, Any]):
|
|
||||||
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
||||||
client_socket.connect(SERVER_ADDRESS)
|
|
||||||
|
|
||||||
try:
|
|
||||||
message:bytes = msgpack.packb(data)
|
|
||||||
|
|
||||||
client_socket.sendall(message)
|
|
||||||
|
|
||||||
rx = client_socket.recv(1024)
|
|
||||||
if data:
|
|
||||||
print(f"Received message: {rx.decode('utf-8').strip()}")
|
|
||||||
else:
|
|
||||||
print("Connection closed")
|
|
||||||
return
|
|
||||||
except ConnectionResetError:
|
|
||||||
print("Connection reset")
|
|
||||||
finally:
|
|
||||||
client_socket.close()
|
|
||||||
print("Connection closed")
|
|
||||||
|
|
||||||
Loading…
Add table
Reference in a new issue