I have a hexadecimal string "89-50-4E-47-0D-0A-1A-0A-00-00-00-0D-49"
to be specific this will contain the data of an image.
I want to convert it to a Numpy array or possibly reconstruct an OpenCV image from the said data.
The width and height will also be supplied so the dimensions of the Numpy array is known.
How can I construct a Numpy array from the above string?
We could use np.fromiter
, and cast the individual strings to hexadecimal, using the base
argument in int
, and then to integer type using the dtype
argument in np.fromiter
:
s = "89-50-4E-47-0D-0A-1A-0A-00-00-00-0D-49"
np.fromiter((int(x, 16) for x in s.split('-')), dtype=np.int32)
# array([137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73])