0

I'm a novice python programmer and I'm trying to open an image file and read it in as binary data, which I then want to convert to numpy array. I assumed by using the below code, it would read the raw bytes in as a list that is delimited somehow, which would easily translate to an array.

try:
    with open(TEST_IMG, 'rb') as image_fd:
        img_line = image_fd.read()
except:
    print("unable to open image file")

The issue, I think, is the read() method returns the entire file as binary data but it seems to read it in as a list obj that holds the entire file as one member of a list. So when i try

img_line = np.array(img_line)

It just creates a single np array with a single element that holds all of the raw binary list. I know I could use pillow or cv2 but I really want to understand how these datatypes work in Python so I can manipulate file data no matter the origin.

Is my assumption correct about the data read in not being a list of individual bytes? And if so how would I break the list up into individual bytes so that I can convert to an np array?

Thanks

  • Let image software take care of those details. There are different image formats, some with compresion, and some propriatary. A numpy array is a numeric representation of the image, not raw bytes. What is `type(img_line)`? Probably a `bytestring`.. There is a numpy `from_string` (or something like that), but I doubt if that will help much. – hpaulj Apr 17 '21 at 06:12
  • arr = np.fromfile(TEST_IMG, 'uint8')` should give you an array of numbers. But what now? – hpaulj Apr 17 '21 at 06:56

0 Answers0