Beginner OpenCV issue

My problem involves reading RGBA data over from a socket in python.

    imgWidth = int.from_bytes(connection.recv(4))
    imgHeight = int.from_bytes(connection.recv(4))
    imgBufferSize = int.from_bytes(connection.recv(4))

    image = Image.frombytes('RGBA', (imgWidth,imgHeight), connection.recv(imgBufferSize), 'raw')

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.

no opencv code in your question, unfortunately…

Wow! How embarrassing! :cold_sweat: I negelected to see :

from PIL import Image

At the top of this class.

So if I can reword my question, how would I use OpenCV to take this raw data and make an RGBA image out of it (or am I way off course here)?

use numpy’s frombuffer

1 Like

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

So am I wrong in assuming that

connection.recv(imgBufferSize)

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 ?

Thank you

yes, exactly. slice & dice.

(and again, neither an opencv, nor a PIL problem, more a general “read from network” one)

you also got something else wrong: opencv {aka cv(2))] works with numpy arrays, PIL doesnt.

I think I have a path forward.

Thank you for your patience with a Python neophyte.

Future posts will be more relevant

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).