0

I am trying to build an online server with tornado and opencv for some image processing work.

Now I am able to load the image from the byte string in the html form uploaded according to this answer here: Python OpenCV load image from byte string

nparr = np.fromstring(img_str, np.uint8)

The problem with this is I have to specify whether the file is uint8/uint16 when np reads it. In this scenario, the user may upload both uint8 and uint16 (grey scale) images so this function probabily does not work well because I do not know how to get the data type information before decoding the image.

An example of the byte string is like this

{bytes} b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00\xc0\x00\xc0\x00\x00\xff\xe1\x0bnExif\x00\x00MM\x00*\x00\x00\x00\x08\x00\x12\x00\xfe

I know cv2.imread could detect the channel depth automatically but it does not work with byte string. Is there any other cv2 function that may take byte string? Or is it possible to let numpy detect the data type automatically?

Thanks in advance!

rqi14
  • 53
  • 2
  • 6
  • you are looking for [imdecode](https://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html?#imdecode) – api55 Jun 18 '18 at 11:40
  • If I do cv2.imdecode(byte_string, cv2.IMREAD_UNCHANGED), I get TypeError: buf is not a numpy array, neither a scalar – rqi14 Jun 18 '18 at 11:43
  • even with np.uint8? byte_string has to be an numpy array type np.uint8. It doesn't matter if the image is 16 bit, a byte is always 8 bit and it requires a byte array – api55 Jun 18 '18 at 12:07
  • You are absolutely right. Thanks so much! – rqi14 Jun 18 '18 at 12:15

0 Answers0