Ask Your Question

How do I read raw RGB data with Python?

asked 2020-11-25 05:54:27 -0500

I've got raw data with each byte representing the R, G or B values. No header, no compression. I need to read in this data fro processing. I've searched a lot but either they refer to Mat which my cv2 module doesn't have, or with numpy and cv2.imdecode that returns None for me:

npdata = np.frombuffer(img, dtype=np.uint8) cv2.imdecode(npdata, cv2.IMREAD_COLOR)

I'm using Python 3.8 and opencv 4.5.0 on Arch Linux.

edit retag flag offensive close merge delete



where do you have the data ?

I need to read in this data fro processing.

explain, please.

and if it is raw data, why try to decode it ? that's for sure the wrong idea.

berak gravatar imageberak ( 2020-11-25 06:51:01 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2020-11-25 11:11:59 -0500

crackwitz gravatar image

np.frombuffer is the right direction. that's for raw bitmap data without a header.

imdecode is not. imdecode/imread is for image containers (.bmp, ...)

you use np.frombuffer, then reshape the data as needed: npdata.shape = (height, width, numchannels).

post your data for more precise instructions.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2020-11-25 05:54:27 -0500

Seen: 68 times

Last updated: Nov 25 '20