Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MhmdRyhn/492af75b96bec3a6c675c9f320d9e5f6 to your computer and use it in GitHub Desktop.
Save MhmdRyhn/492af75b96bec3a6c675c9f320d9e5f6 to your computer and use it in GitHub Desktop.
# Taken from: https://stackoverflow.com/a/52969463/5600965
from PIL import Image
def pad_image(original_image, **kwargs):
width, height = 200, 64
ratio_width = width / original_image.width
ratio_height = height / original_image.height
if ratio_width <= ratio_height:
# It must be fixed by width
resize_width = width
resize_height = round(ratio_width * original_image.height)
else:
# Fixed by height
resize_width = round(ratio_height * original_image.width)
resize_height = height
image_resize = original_image.resize((resize_width, resize_height), Image.ANTIALIAS)
background = Image.new('RGBA', (width, height), (0, 0, 0, 0))
offset = (round((width - resize_width) / 2), round((height - resize_height) / 2))
background.paste(image_resize, offset)
return background
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment