Kai
Kai

Reputation: 45

Use PIL to convert a grayscale image to a (1, H, W) numpy array

Using PIL to convert a RGB image to a (H, W, 3) numpy array is very fast.

im = np.array(PIL.open(path))

However, I cannot find a fast way to convert a grayscale image to a (H, W, 1) array. I tried two approaches but they are both much slower than above:

im = np.array(PIL.open(path)) # return an (H, W) array
im = np.expand_dims(im, axis=0)
im = im.astype(int)

This approach is slow too:

img = PIL.open(path)
im = np.array(img.getdata()).reshape(img.size[1], img.size[0], 1)

Please advice...

Upvotes: 1

Views: 1824

Answers (1)

Divakar
Divakar

Reputation: 221574

You can use np.asarray() to get the array view, then append a new axis with None/np.newaxis and then use type conversion with copy set to False (in case you were converting from same dtype to save on memory) -

im = np.asarray(PIL.open(path))
im_out = im[None].astype(dtype=int, copy=False)

This appends new axis at the start, resulting in (1,H,W) as the output array shape. To do so, for the end to get an array shape of (H,W,1), do : im[...,None] instead of im[None].


A simpler way would be -

im_out = np.asarray(img, dtype=int)[None]

If the input is already in uint8 dtype and we want an output array of the same dtype, use dtype=np.uint8 and that should be pretty fast.

Upvotes: 1

Related Questions