I’m trying to automatically load and image from URL and then load it to the numpy matrix. For this I need to use requests and opencv library. It works in the correct way for standard images that are encoded like uint8 and it corrupts the uint16 imaged because of overflowing the values:
r = requests.get(url, headers=headers, timeout=timeout)
img_bin = r.content
image_data = np.asarray(bytearray(img_bin), dtype="uint8")
image = cv2.imdecode(image_data, flag)
Loading uint16 image from the local drive is working correctly:
image = cv2.imread(png_u16_filepath, cv2.IMREAD_UNCHANGED)
image.dtype -> np.uint16
image = cv2.imread(png_u8_filepath, cv2.IMREAD_UNCHANGED)
image.dtype -> np.uint8
However, I need to load the image from the internet and then from memory, I cannot write them to the image file locally.
- Is it possible to know if the image file is uint8 or uint16 from requests.img_bin or in any other way? The images behind the URL can be both uint8 or uint16 images.
- I also try np.asarray(bytearray(img_bin), dtype=”uint16″) but it leads to the error: ValueError: buffer size must be a multiple of element size. Is there a way to convert img_bin to correct numpy image that is np.uint16 via opencv lib?
Thank you for your help