It has been confirmed that the data is flowing correctly over the network. In other words, imgWidth, imgHeight, imgBufferSize and the raw data is coming over correctly.
I am sorry that I do not have the exact error message available.
I was hoping for such a simple task that some could point out what I am doing wrong.
also be aware, that real life [udp|tcp] packets will be fragmented
(by any device in your way)
while you can send a 10 mb buffer with a single call, you have to retrieve the output in a loop, assembling much smaller buffers (like 1k) until you have read the final bytecount
you never told us, what went wrong with our original, PIL based effort, but i guess, this is the problem
will not block until all ‘imgBufferSize’ bytes are read ?
If that is true, then that explains part of my issue.
To clarify after using nparr = np.frombuffer(connection.recv(imgBufferSize), dtype="uint8")
I received this error :
File “.\tcp_test.py”, line 31, in
image = Image.frombuffer('RGBA', (imgWidth,imgHeight), nparr)
File "C:\Python38\lib\site-packages\PIL\Image.py", line 2729, in frombuffer
im = im._new(core.map_buffer(data, size, decoder_name, 0, args))
ValueError: buffer is not large enough
If you think this is the root my issue, can you please share how I do need to be reading that socket data ?
recv() may return as much as you asked for, or less. it will block until there’s something it can give you. if it returns 0, the connection was closed.
same applies to send(). it may have sent all you gave it, or less (or nothing and an error). then you should send() again on the remainder, until nothing is left.
both functions have documentation… if python’s docs aren’t precise enough, there are “manpages” for both calls, easily accessible on the web.
python bytes objects can be concatenated with + or += or you can collect all pieces in a list and then b''.join(thatlist).