Book Image

OpenCV for Secret Agents

By : Joseph Howse
Book Image

OpenCV for Secret Agents

By: Joseph Howse

Overview of this book

Table of Contents (15 chapters)
OpenCV for Secret Agents
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Preparing images and resources for the app


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...