22 lines
634 B
Python
22 lines
634 B
Python
import numpy as np
|
|
|
|
|
|
class Packet:
|
|
def __init__(self, x: int, y: int, array: np.ndarray):
|
|
self.x = x
|
|
self.y = y
|
|
self.array = array
|
|
|
|
def to_bytestr(self) -> bytes:
|
|
bytestr = b""
|
|
bytestr += self.x.to_bytes(length=4, signed = False)
|
|
bytestr += self.y.to_bytes(length=4, signed = False)
|
|
bytestr += self.array.tobytes()
|
|
return bytestr
|
|
|
|
def from_bytes(b: bytes) -> Packet:
|
|
x = int.from_bytes(b[0:4], signed = False)
|
|
y = int.from_bytes(b[4:8], signed = False)
|
|
array = np.frombuffer(b[8:], np.uint8).reshape(16, 16, 3)
|
|
|
|
return Packet(x, y, array) |