def load_image(filepath, as_grey=False, dtype='uint8', no_alpha=True):
"""
Load image as numpy array from given filepath.
Supported formats: gif, png, jpg, bmp, tif, npy
>>> img = load_image('tests/data/img_formats/nut_color.jpg')
>>> shapestr(img)
'213x320x3'
:param string filepath: Filepath to image file or numpy array.
:param bool as_grey:
:return: numpy array with shapes
(h, w) for grayscale or monochrome,
(h, w, 3) for RGB (3 color channels in last axis)
(h, w, 4) for RGBA (for no_alpha = False)
(h, w, 3) for RGBA (for no_alpha = True)
pixel values are in range [0,255] for dtype = uint8
:rtype: numpy ndarray
"""
if filepath.endswith('.npy'): # image as numpy array
arr = np.load(filepath).astype(dtype)
arr = rgb2gray(arr) if as_grey else arr
else:
# img_num=0 due to
# https://github.com/scikit-image/scikit-image/issues/2406
arr = ski.imread(filepath, as_grey=as_grey, img_num=0).astype(dtype)
if arr.ndim == 3 and arr.shape[2] == 4 and no_alpha:
arr = arr[..., :3] # cut off alpha channel
return arr
评论列表
文章目录