Skip to content

Instantly share code, notes, and snippets.

@NicholasBallard
Last active August 16, 2020 05:08
Show Gist options
  • Save NicholasBallard/02d470e2f8def44f16f8d25c7c1608a4 to your computer and use it in GitHub Desktop.
Save NicholasBallard/02d470e2f8def44f16f8d25c7c1608a4 to your computer and use it in GitHub Desktop.
from PIL import Image
def resize(fp: str, scale: Union[float, int]) -> np.ndarray:
""" Resize an image maintaining its proportions
Args:
fp (str): Path argument to image file
scale (Union[float, int]): Percent as whole number of original image. eg. 53
Returns:
image (np.ndarray): Scaled image
"""
_scale = lambda dim, s: int(dim * s / 100)
im = Image.open(fp)
width, height = im.size
new_width: int = _scale(width, scale)
new_height: int = _scale(height, scale)
new_dim: tuple = (new_width, new_height)
return im.resize(new_dim)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment