Last active
August 22, 2021 11:22
-
-
Save NicholasBallard/b454eb6cd2e6f66a0346154a435b420b to your computer and use it in GitHub Desktop.
opencv_resize.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
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: np.ndarray = cv2.imread(fp) | |
height, width, channels = im.shape | |
new_width: int = _scale(width, scale) | |
new_height: int = _scale(height, scale) | |
new_dim: tuple = (new_width, new_height) | |
return cv2.resize(src=im, dsize=new_dim, interpolation=cv2.INTER_LINEAR) |
@rkosti Thanks for your help fixing the code. Good eagle eyes spotting the transposition.
You're welcome. I realized that my screenshot didn't upload last time. I updated the comment.
Happy Coding :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@rkosti Thank you for looking at the code!
It does appear the
dsize
in OpenCV is (width, height).https://docs.opencv.org/master/da/d54/group__imgproc__transform.html#ga47a974309e9102f5f08231edc7e7529d