def pad(img, padding, fill=<NUM_LIT:0>, padding_mode='<STR_LIT>'):
if not _is_pil_image(img):<EOL><INDENT>raise TypeError('<STR_LIT>'.format(type(img)))<EOL><DEDENT>if not isinstance(padding, (numbers.Number, tuple)):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if not isinstance(fill, (numbers.Number, str, tuple)):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if not isinstance(padding_mode, str):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if isinstance(padding, Sequence) and len(padding) not in [<NUM_LIT:2>, <NUM_LIT:4>]:<EOL><INDENT>raise ValueError("<STR_LIT>" +<EOL>"<STR_LIT>".format(len(padding)))<EOL><DEDENT>assert padding_mode in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'],'<STR_LIT>'<EOL>if padding_mode == '<STR_LIT>':<EOL><INDENT>if img.mode == '<STR_LIT:P>':<EOL><INDENT>palette = img.getpalette()<EOL>image = ImageOps.expand(img, border=padding, fill=fill)<EOL>image.putpalette(palette)<EOL>return image<EOL><DEDENT>return ImageOps.expand(img, border=padding, fill=fill)<EOL><DEDENT>else:<EOL><INDENT>if isinstance(padding, int):<EOL><INDENT>pad_left = pad_right = pad_top = pad_bottom = padding<EOL><DEDENT>if isinstance(padding, Sequence) and len(padding) == <NUM_LIT:2>:<EOL><INDENT>pad_left = pad_right = padding[<NUM_LIT:0>]<EOL>pad_top = pad_bottom = padding[<NUM_LIT:1>]<EOL><DEDENT>if isinstance(padding, Sequence) and len(padding) == <NUM_LIT:4>:<EOL><INDENT>pad_left = padding[<NUM_LIT:0>]<EOL>pad_top = padding[<NUM_LIT:1>]<EOL>pad_right = padding[<NUM_LIT:2>]<EOL>pad_bottom = padding[<NUM_LIT:3>]<EOL><DEDENT>if img.mode == '<STR_LIT:P>':<EOL><INDENT>palette = img.getpalette()<EOL>img = np.asarray(img)<EOL>img = np.pad(img, ((pad_top, pad_bottom), (pad_left, pad_right)), padding_mode)<EOL>img = Image.fromarray(img)<EOL>img.putpalette(palette)<EOL>return img<EOL><DEDENT>img = np.asarray(img)<EOL>if len(img.shape) == <NUM_LIT:3>:<EOL><INDENT>img = np.pad(img, ((pad_top, pad_bottom), (pad_left, pad_right), (<NUM_LIT:0>, <NUM_LIT:0>)), padding_mode)<EOL><DEDENT>if len(img.shape) == <NUM_LIT:2>:<EOL><INDENT>img = np.pad(img, ((pad_top, pad_bottom), (pad_left, pad_right)), padding_mode)<EOL><DEDENT>return Image.fromarray(img)<EOL><DEDENT>
r"""Pad the given PIL Image on all sides with specified padding mode and fill value.

    Args:
        img (PIL Image): Image to be padded.
        padding (int or tuple): Padding on each border. If a single int is provided this
            is used to pad all borders. If tuple of length 2 is provided this is the padding
            on left/right and top/bottom respectively. If a tuple of length 4 is provided
            this is the padding for the left, top, right and bottom borders
            respectively.
        fill: Pixel fill value for constant fill. Default is 0. If a tuple of
            length 3, it is used to fill R, G, B channels respectively.
            This value is only used when the padding_mode is constant
        padding_mode: Type of padding. Should be: constant, edge, reflect or symmetric. Default is constant.

            - constant: pads with a constant value, this value is specified with fill

            - edge: pads with the last value on the edge of the image

            - reflect: pads with reflection of image (without repeating the last value on the edge)

                       padding [1, 2, 3, 4] with 2 elements on both sides in reflect mode
                       will result in [3, 2, 1, 2, 3, 4, 3, 2]

            - symmetric: pads with reflection of image (repeating the last value on the edge)

                         padding [1, 2, 3, 4] with 2 elements on both sides in symmetric mode
                         will result in [2, 1, 1, 2, 3, 4, 4, 3]

    Returns:
        PIL Image: Padded image.