from PyQt5 import QtWidgets
import cv2
[docs]
def select_file(title, dir_path, file_filter):
"""
Find the file path from the directory computer.
Args:
title: the title window of open dialog
file_filter: determine the specific file want to search
dir_path: Navigate to specific directory
return:
file_path: location
"""
options = QtWidgets.QFileDialog.DontUseNativeDialog
file_path, _ = QtWidgets.QFileDialog.getOpenFileName(None, title, dir_path,
file_filter,
options=options)
return file_path
[docs]
def resize_image(image, width):
"""
Resize image original with our size we want
Args:
image (): image original
width (): image width we want
Returns:
result: image has been resize
"""
h, w = image.shape[:2]
r = width / float(w)
hi = round(h * r)
result = cv2.resize(image, (width, hi),
interpolation=cv2.INTER_AREA)
return result
[docs]
def calculate_height(image, image_width):
"""
Calculate the height image with the same ratio with the size original image.
Args:
image: original image
image_width: size image we want
Returns:
height: height image
"""
h, w = image.shape[:2]
r = image_width / float(w)
height = round(h * r)
return height
[docs]
def read_image(image_path):
"""
Reading the image from given file path using openCV.
Args:
image_path : The path of image file
return:
Image: load image
"""
image = cv2.imread(image_path)
if image is None:
raise FileNotFoundError("`{}` not cannot be loaded".format(image_path))
return image
[docs]
def rotate(src, angle, center=None, scale=1.0):
"""
Turn an image in a clockwise or counterclockwise direction.
Args:
src: original image
angle: the value angle for turn the image
center: determine the specific coordinate to rotate image
scale: scale image
Returns:
dst image: rotated image
"""
h, w = src.shape[:2]
if center is None:
center = (w / 2, h / 2)
m = cv2.getRotationMatrix2D(center, angle, scale)
rotated = cv2.warpAffine(src, m, (w, h))
return rotated