if isinstance(im, np.ndarray):
if len(im.shape) == 2:
im = np.expand_dims(im, axis=0)
if im.shape[0] == 1:
// Converting an image with depth = 1 to depth = 3, repeating the same values
// For some reason PIL complains when I want to save channel image as jpg without
// additional format in the .save()
im = np.repeat(im, 3, axis=0)
// Convert to values to range 1-255 and W,H, D
// A bandaid fix to an issue with gradcam
if im.shape[0] == 3 and np.max(im) == 1:
im = im.transpose(1, 2, 0) * 255
elif im.shape[0] == 3 and np.max(im) > 1:
im = im.transpose(1, 2, 0)
im = Image.fromarray(im.astype(np.uint8))
im.save(path)
def preprocess_image(pil_im, resize_im=True):
After Change
im_as_arr (Numpy array): Matrix of shape DxWxH
path (str): Path to the image
if isinstance(im, (np.ndarray, np.generic)):
im = format_np_output(im)
im = Image.fromarray(im)
im.save(path)