def to_pil_image(pic, mode=None):
if not(isinstance(pic, torch.Tensor) or isinstance(pic, np.ndarray)):<EOL><INDENT>raise TypeError('<STR_LIT>'.format(type(pic)))<EOL><DEDENT>elif isinstance(pic, torch.Tensor):<EOL><INDENT>if pic.ndimension() not in {<NUM_LIT:2>, <NUM_LIT:3>}:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(pic.ndimension()))<EOL><DEDENT>elif pic.ndimension() == <NUM_LIT:2>:<EOL><INDENT>pic = pic.unsqueeze(<NUM_LIT:0>)<EOL><DEDENT><DEDENT>elif isinstance(pic, np.ndarray):<EOL><INDENT>if pic.ndim not in {<NUM_LIT:2>, <NUM_LIT:3>}:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(pic.ndim))<EOL><DEDENT>elif pic.ndim == <NUM_LIT:2>:<EOL><INDENT>pic = np.expand_dims(pic, <NUM_LIT:2>)<EOL><DEDENT><DEDENT>npimg = pic<EOL>if isinstance(pic, torch.FloatTensor):<EOL><INDENT>pic = pic.mul(<NUM_LIT:255>).byte()<EOL><DEDENT>if isinstance(pic, torch.Tensor):<EOL><INDENT>npimg = np.transpose(pic.numpy(), (<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:0>))<EOL><DEDENT>if not isinstance(npimg, np.ndarray):<EOL><INDENT>raise TypeError('<STR_LIT>' +<EOL>'<STR_LIT>'.format(type(npimg)))<EOL><DEDENT>if npimg.shape[<NUM_LIT:2>] == <NUM_LIT:1>:<EOL><INDENT>expected_mode = None<EOL>npimg = npimg[:, :, <NUM_LIT:0>]<EOL>if npimg.dtype == np.uint8:<EOL><INDENT>expected_mode = '<STR_LIT:L>'<EOL><DEDENT>elif npimg.dtype == np.int16:<EOL><INDENT>expected_mode = '<STR_LIT>'<EOL><DEDENT>elif npimg.dtype == np.int32:<EOL><INDENT>expected_mode = '<STR_LIT:I>'<EOL><DEDENT>elif npimg.dtype == np.float32:<EOL><INDENT>expected_mode = '<STR_LIT:F>'<EOL><DEDENT>if mode is not None and mode != expected_mode:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>.format(mode, np.dtype, expected_mode))<EOL><DEDENT>mode = expected_mode<EOL><DEDENT>elif npimg.shape[<NUM_LIT:2>] == <NUM_LIT:2>:<EOL><INDENT>permitted_2_channel_modes = ['<STR_LIT>']<EOL>if mode is not None and mode not in permitted_2_channel_modes:<EOL><INDENT>raise ValueError("<STR_LIT>".format(permitted_2_channel_modes))<EOL><DEDENT>if mode is None and npimg.dtype == np.uint8:<EOL><INDENT>mode = '<STR_LIT>'<EOL><DEDENT><DEDENT>elif npimg.shape[<NUM_LIT:2>] == <NUM_LIT:4>:<EOL><INDENT>permitted_4_channel_modes = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']<EOL>if mode is not None and mode not in permitted_4_channel_modes:<EOL><INDENT>raise ValueError("<STR_LIT>".format(permitted_4_channel_modes))<EOL><DEDENT>if mode is None and npimg.dtype == np.uint8:<EOL><INDENT>mode = '<STR_LIT>'<EOL><DEDENT><DEDENT>else:<EOL><INDENT>permitted_3_channel_modes = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']<EOL>if mode is not None and mode not in permitted_3_channel_modes:<EOL><INDENT>raise ValueError("<STR_LIT>".format(permitted_3_channel_modes))<EOL><DEDENT>if mode is None and npimg.dtype == np.uint8:<EOL><INDENT>mode = '<STR_LIT>'<EOL><DEDENT><DEDENT>if mode is None:<EOL><INDENT>raise TypeError('<STR_LIT>'.format(npimg.dtype))<EOL><DEDENT>return Image.fromarray(npimg, mode=mode)<EOL>
Convert a tensor or an ndarray to PIL Image.

    See :class:`~torchvision.transforms.ToPILImage` for more details.

    Args:
        pic (Tensor or numpy.ndarray): Image to be converted to PIL Image.
        mode (`PIL.Image mode`_): color space and pixel depth of input data (optional).

    .. _PIL.Image mode: https://pillow.readthedocs.io/en/latest/handbook/concepts.html#concept-modes

    Returns:
        PIL Image: Image converted to PIL Image.