littlefish/imageutil.py
def resize_pad_image(image, dest_w, dest_h, pad_with_transparent=False):
    """
    Resize the image and pad to the correct aspect ratio.
    :param image: PIL.Image
    :param dest_w: Target width
    :param dest_h: Target height
    :param pad_with_transparent: If True, make additional padding transparent

    :return: Scaled and padded image
    """
    dest_w = float(dest_w)
    dest_h = float(dest_h)
    dest_ratio = dest_w / dest_h

    # Calculate the apect ratio of the image
    src_w = float(image.size[0])
    src_h = float(image.size[1])
    src_ratio = src_w / src_h

    if src_ratio < dest_ratio:
        # Image is tall and thin - we need to scale to the right height and then pad
        scale = dest_h / src_h
        scaled_h = dest_h
        scaled_w = src_w * scale

        offset = (int((dest_w - scaled_w) / 2), 0)

    else:
        # Image is short and wide - we need to scale to the right height and then crop
        scale = dest_w / src_w
        scaled_w = dest_w
        scaled_h = src_h * scale

        offset = (0, int((dest_h - scaled_h) / 2))

    scaled_image = image.resize((int(scaled_w), int(scaled_h)), PIL.Image.ANTIALIAS)
    # Normally we will want to copy the source mode for the destination image, but in some
    # cases the source image will use a Palletted (mode=='P') in which case we need to change
    # the mode
    mode = scaled_image.mode
    log.debug('Padding image with mode: "{}"'.format(mode))
    if pad_with_transparent and mode != 'RGBA':
        old_mode = mode
        mode = 'RGBA'
        scaled_image = scaled_image.convert(mode)
        log.debug('Changed mode from "{}" to "{}"'.format(old_mode, mode))

    elif mode == 'P':
        if 'transparency' in scaled_image.info:
            mode = 'RGBA'
        else:
            mode = 'RGB'

        scaled_image = scaled_image.convert(mode)
        log.debug('Changed mode from "P" to "{}"'.format(mode))
    
    if pad_with_transparent:
        pad_colour = (255, 255, 255, 0)
    else:
        # Get the pixel colour for coordinate (0,0)
        pixels = scaled_image.load()
        pad_colour = pixels[0, 0]
    
    padded_image = PIL.Image.new(mode, (int(dest_w), int(dest_h)), pad_colour)
    padded_image.paste(scaled_image, offset)

    return padded_image
def resize_pad_image(image, dest_w, dest_h, pad_with_transparent=False):
    """
    Resize the image and pad to the correct aspect ratio.
    :param image: PIL.Image
    :param dest_w: Target width
    :param dest_h: Target height
    :param pad_with_transparent: If True, make additional padding transparent

    :return: Scaled and padded image
    """
    dest_w = float(dest_w)
    dest_h = float(dest_h)
    dest_ratio = dest_w / dest_h

    # Calculate the apect ratio of the image
    src_w = float(image.size[0])
    src_h = float(image.size[1])
    src_ratio = src_w / src_h

    if src_ratio < dest_ratio:
        # Image is tall and thin - we need to scale to the right height and then pad
        scale = dest_h / src_h
        scaled_h = dest_h
        scaled_w = src_w * scale

        offset = (int((dest_w - scaled_w) / 2), 0)

    else:
        # Image is short and wide - we need to scale to the right height and then crop
        scale = dest_w / src_w
        scaled_w = dest_w
        scaled_h = src_h * scale

        offset = (0, int((dest_h - scaled_h) / 2))

    scaled_image = image.resize((int(scaled_w), int(scaled_h)), PIL.Image.ANTIALIAS)
    # Normally we will want to copy the source mode for the destination image, but in some
    # cases the source image will use a Palletted (mode=='P') in which case we need to change
    # the mode
    mode = scaled_image.mode
    log.debug('Padding image with mode: "{}"'.format(mode))
    if pad_with_transparent and mode != 'RGBA':
        old_mode = mode
        mode = 'RGBA'
        scaled_image = scaled_image.convert(mode)
        log.debug('Changed mode from "{}" to "{}"'.format(old_mode, mode))

    elif mode == 'P':
        if 'transparency' in scaled_image.info:
            mode = 'RGBA'
        else:
            mode = 'RGB'

        scaled_image = scaled_image.convert(mode)
        log.debug('Changed mode from "P" to "{}"'.format(mode))
    
    if pad_with_transparent:
        pad_colour = (255, 255, 255, 0)
    else:
        # Get the pixel colour for coordinate (0,0)
        pixels = scaled_image.load()
        pad_colour = pixels[0, 0]
    
    padded_image = PIL.Image.new(mode, (int(dest_w), int(dest_h)), pad_colour)
    padded_image.paste(scaled_image, offset)

    return padded_image
Resize the image and pad to the correct aspect ratio.
    :param image: PIL.Image
    :param dest_w: Target width
    :param dest_h: Target height
    :param pad_with_transparent: If True, make additional padding transparent

    :return: Scaled and padded image