import numpy as np import cv2 def translate(image, x, y): # Define the translation matrix and perform the translation matrix = np.float32([[1, 0, x], [0, 1, y]]) shifted = cv2.warpAffine(image, matrix, (image.shape[1], image.shape[0])) # Return the translated image return shifted def rotate(image, angle, center=None, scale=1.0): # Grab the dimensions of the image (h, w) = image.shape[:2] # If the center is None, initialize it as the center of the image if center is None: center = (w // 2, h // 2) # Perform the rotation matrix = cv2.getRotationMatrix2D(center, angle, scale) rotated = cv2.warpAffine(image, matrix, (w, h)) # Return the rotated image return rotated def resize(image, width=None, height=None, inter=cv2.INTER_AREA): # Grab the image size (h, w) = image.shape[:2] # If both the width and height are None, then return the original image if width is None and height is None: return image # Check to see if the width is None if width is None: # Calculate the ratio of the height and construct the dimensions r = height / float(h) dim = (int(w * r), height) # Otherwise, the height is None else: # Calculate the ratio of the width and construct the dimensions r = width / float(w) dim = (width, int(h * r)) # Resize the image resized = cv2.resize(image, dim, interpolation=inter) # Return the resized image return resized