Skip to content

Instantly share code, notes, and snippets.

@BIGBALLON
Last active June 28, 2022 19:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save BIGBALLON/cb6ab73f6aaaa068ab6756611bb324b2 to your computer and use it in GitHub Desktop.
Save BIGBALLON/cb6ab73f6aaaa068ab6756611bb324b2 to your computer and use it in GitHub Desktop.
from PIL import Image, ImageOps
def padding(img, expected_size):
desired_size = expected_size
delta_width = desired_size - img.size[0]
delta_height = desired_size - img.size[1]
pad_width = delta_width // 2
pad_height = delta_height // 2
padding = (pad_width, pad_height, delta_width - pad_width, delta_height - pad_height)
return ImageOps.expand(img, padding)
def resize_with_padding(img, expected_size):
img.thumbnail((expected_size[0], expected_size[1]))
# print(img.size)
delta_width = expected_size[0] - img.size[0]
delta_height = expected_size[1] - img.size[1]
pad_width = delta_width // 2
pad_height = delta_height // 2
padding = (pad_width, pad_height, delta_width - pad_width, delta_height - pad_height)
return ImageOps.expand(img, padding)
if __name__ == "__main__":
img = Image.open("./demo.jpg")
print(img)
img = resize_with_padding(img, (500, 400))
print(img.size)
img.show()
img.save("resized_img.jpg")
@BIGBALLON
Copy link
Author

BIGBALLON commented Mar 17, 2020

  • raw image:

demo

  • after resizing with padding:

resized_img

@RamSasanka
Copy link

Thanks for the code, how can we set White color in padding instead of Black.

@BIGBALLON
Copy link
Author

BIGBALLON commented May 20, 2021

Hi, @RamSasanka, you can use fill = (255,255,255) to set white color in padding instead of black.

def expand(image, border=0, fill=0):
    """
    Add border to the image

    :param image: The image to expand.
    :param border: Border width, in pixels.
    :param fill: Pixel fill value (a color value).  Default is 0 (black).
    :return: An image.
    """
    left, top, right, bottom = _border(border)
    width = left + image.size[0] + right
    height = top + image.size[1] + bottom
    out = Image.new(image.mode, (width, height), _color(fill, image.mode))
    out.paste(image, (left, top))
    return out
from PIL import Image, ImageOps


def padding(img, expected_size):
    desired_size = expected_size
    delta_width = desired_size - img.size[0]
    delta_height = desired_size - img.size[1]
    pad_width = delta_width // 2
    pad_height = delta_height // 2
    padding = (
        pad_width,
        pad_height,
        delta_width - pad_width,
        delta_height - pad_height,
    )
    return ImageOps.expand(img, padding)


def resize_with_padding(img, expected_size, fill=0):
    img.thumbnail((expected_size[0], expected_size[1]))
    # print(img.size)
    delta_width = expected_size[0] - img.size[0]
    delta_height = expected_size[1] - img.size[1]
    pad_width = delta_width // 2
    pad_height = delta_height // 2
    padding = (
        pad_width,
        pad_height,
        delta_width - pad_width,
        delta_height - pad_height,
    )
    return ImageOps.expand(img, padding, fill=fill)


if __name__ == "__main__":
    img = Image.open("./demo.jpg")
    print(img)
    img = resize_with_padding(img, (500, 400), (255, 255, 255))
    print(img.size)
    img.show()
    img.save("resized_img.jpg")

@BIGBALLON
Copy link
Author

BIGBALLON commented May 20, 2021

  • fill = (255,255,255) white

resized_img

  • fill = (255,0,0) red

resized_img

@RamSasanka
Copy link

Thanks a bunch Will !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment