def prepare_for_display(npy_img):
 if (npy_img.ndim < 2):
  raise ValueError('Image must be 2D or 3D array')
 height = npy_img.shape[0]
 width = npy_img.shape[1]
 out = np.empty((height, width, 3), dtype=np.uint8)
 npy_img = img_as_ubyte(npy_img)
 if ((npy_img.ndim == 2) or ((npy_img.ndim == 3) and (npy_img.shape[2] == 1))):
  npy_plane = npy_img.reshape((height, width))
  out[:, :, 0] = npy_plane
  out[:, :, 1] = npy_plane
  out[:, :, 2] = npy_plane
 elif (npy_img.ndim == 3):
  if ((npy_img.shape[2] == 3) or (npy_img.shape[2] == 4)):
   out[:, :, :3] = npy_img[:, :, :3]
  else:
   raise ValueError('Image must have 1, 3, or 4 channels')
 else:
  raise ValueError('Image must have 2 or 3 dimensions')
 return out

def prepare_for_display ( npy_img ) : if ( npy_img . ndim < 2 ) : raise ValueError ( 'Image must be 2D or 3D array' ) height = npy_img . shape [ 0 ] width = npy_img . shape [ 1 ] out = np . empty ( ( height , width , 3 ) , dtype = np . uint8 ) npy_img = img_as_ubyte ( npy_img ) if ( ( npy_img . ndim == 2 ) or ( ( npy_img . ndim == 3 ) and ( npy_img . shape [ 2 ] == 1 ) ) ) : npy_plane = npy_img . reshape ( ( height , width ) ) out [ : , : , 0 ] = npy_plane out [ : , : , 1 ] = npy_plane out [ : , : , 2 ] = npy_plane elif ( npy_img . ndim == 3 ) : if ( ( npy_img . shape [ 2 ] == 3 ) or ( npy_img . shape [ 2 ] == 4 ) ) : out [ : , : , : 3 ] = npy_img [ : , : , : 3 ] else : raise ValueError ( 'Image must have 1, 3, or 4 channels' ) else : raise ValueError ( 'Image must have 2 or 3 dimensions' ) return out
convert a 2d or 3d numpy array of any dtype into a 3d numpy array with dtype uint8 .