0
votes

Through some http requests I have been able to receive an image in binary form as b'\xff\xd8\xff\xe0\x00\... and with:

with open('image.jpg', 'wb') as out_file:
    out_file.write(binary_content)

where binary_content is a string containing the data received through request I saved an image into a file.

Afterwards I can read this image with OpenCV methods. But I wanted to do a direct pass from binary string to OpenCV Mat without any in-betweens. cv2.decode method didn't work.

2
The image you received is not in binary format. It is in the hexadecimal format.Rahul Kedia

2 Answers

0
votes

io.BytesIO and PIL worked well. Closing this q.

0
votes

If you want to stay in the SciPy ecosystem, then the imageio library (previously part of SciPy) works well.

from imageio import imread

image_array = imread("image_path.jpg")

The code above gives you an uint8 array, if you want a float array, you can cast it easily

from imageio import imread

image_array = imread("image_path.jpg").astype(float)