1

I am making an API that reads an image as a hex string. I need to convert the hex string to an image vector like what you would get from: img = cv2.imread('abc.jpg')

I know I can do this by writing hex string to file and read as an image:

import cv2
image_name = 'Test_image.jpg'

with open(image_name, 'wb') as outfile:
                outfile.write(hex_string)

img = cv2.imread(image_name)

But my question is how to end up with "img" directly from "hex_string" without needing to write/read from file.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Pyders
  • 33
  • 3
  • My hex string was far too large to include in the question, but it starts like this: 0x255044462D312E370D0A25B5B5B5B50D0A312030206F626A0D0A3C3C2F547970652F436174616C6F672F5 – Pyders May 06 '22 at 08:59
  • hex doesn't mean binary. work with binary data. -- you're asking what `imdecode` is – Christoph Rackwitz May 06 '22 at 09:31
  • 1
    Please check if this link is helpful: https://stackoverflow.com/questions/49511753/python-byte-image-to-numpy-array-using-opencv – Jeru Luke May 06 '22 at 09:33
  • 1
    Thanks a lot. The imdecode was exactly what I was looking for. Problem solved. I'll buy you a beer if you ever go to Bergen :) – Pyders May 06 '22 at 11:26

0 Answers0