Alongside
RequestsUtils.py
and ImageSearchSession.py
, let's create another file called ResizeUtils.py
with the following import statements:
import numpy import cv2
For display in a GUI, images often must be resized. One popular mode of resizing is called aspect fill. This means that we want to preserve the image's aspect ratio while changing its larger dimension (width for a landscape image or height for a portrait image) to a certain value. OpenCV does not directly provide this resizing mode but it does provide a function, cv2.resize
, which accepts an image, target dimensions, and optional arguments including an interpolation method. We can write our own function, cvResizeAspectFill
, which accepts an image, maximum size, and preferred interpolation methods for upsizing and downsizing the images. It determines the appropriate arguments for cv2.resize
and passes them along. Here is the implementation:
def cvResizeAspectFill(src, maxSize, upInterpolation...