Compare commits
No commits in common. "f40f57925afa29ab8cd23c47a61d0acad67a0537" and "6c50042361f7f4b7292480d29f67b86ec3a47a0b" have entirely different histories.
f40f57925a
...
6c50042361
5 changed files with 104 additions and 21 deletions
|
|
@ -30,29 +30,15 @@ def bitmap():
|
||||||
img = Image.open(image_stream)
|
img = Image.open(image_stream)
|
||||||
grayscale_img = img.convert('L')
|
grayscale_img = img.convert('L')
|
||||||
bitmap_data = list(grayscale_img.getdata())
|
bitmap_data = list(grayscale_img.getdata())
|
||||||
|
|
||||||
payload = 'P5\n296 152\n255\n'
|
payload = ''.join([chr(i) for i in bitmap_data])
|
||||||
payload += ''.join([chr(i) for i in bitmap_data])
|
|
||||||
print(payload)
|
print(payload)
|
||||||
req = requests.put('http://host.docker.internal:8080/api/tag', data=chunk_gen(bitmap_data), headers={
|
# req = requests.post('http://10.141.142.75/api/tag', data=payload)
|
||||||
'Content-Type': 'image/x-portable-greymap'
|
return jsonify({'payload': payload})
|
||||||
})
|
|
||||||
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:
|
||||||
return jsonify({"error": e}), 500
|
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__':
|
if __name__ == '__main__':
|
||||||
app.run('0.0.0.0', port=80)
|
app.run('0.0.0.0', port=80)
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,15 @@ services:
|
||||||
container_name: backend_service
|
container_name: backend_service
|
||||||
ports:
|
ports:
|
||||||
- "12001:80"
|
- "12001:80"
|
||||||
|
# bitmap_service:
|
||||||
|
# build: ./bitmap_service
|
||||||
|
# container_name: bitmap_service
|
||||||
|
# ports:
|
||||||
|
# - "12002:80"
|
||||||
bitmap_service:
|
bitmap_service:
|
||||||
build: ./bitmap_service
|
build: ./trash
|
||||||
container_name: bitmap_service
|
container_name: trash
|
||||||
ports:
|
ports:
|
||||||
- "12002:80"
|
- "12002:80"
|
||||||
extra_hosts:
|
extra_hosts:
|
||||||
- "host.docker.internal:host-gateway"
|
- "host.docker.internal:host-gateway"
|
||||||
55
trash/app.py
Normal file
55
trash/app.py
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
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)
|
||||||
9
trash/dockerfile
Normal file
9
trash/dockerfile
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
FROM python:3.9
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
RUN pip3 install pillow flask flask-cors requests
|
||||||
|
|
||||||
|
CMD ["python3", "app.py"]
|
||||||
28
trash/tcp.py
Normal file
28
trash/tcp.py
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
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