Let's take a look at how to operate on images using OpenCV-Python. In this recipe, we will see how to load and display an image. We will also look at how to crop, resize, and save an image to an output file.
import sys import cv2 import numpy as np
forest.jpg
, as follows:# Load and display an image -- 'forest.jpg' input_file = sys.argv[1] img = cv2.imread(input_file)
cv2.imshow('Original', img)
# Cropping an image h, w = img.shape[:2] start_row, end_row = int(0.21*h), int(0.73*h) start_col, end_col= int(0.37*w), int(0.92*w)
img_cropped = img[start_row:end_row, start_col:end_col] cv2.imshow('Cropped', img_cropped)
1.3
times its original size and display it:# Resizing an image scaling_factor = 1.3 img_scaled = cv2.resize(img, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_LINEAR) cv2.imshow('Uniform resizing', img_scaled)
img_scaled = cv2.resize(img, (250, 400), interpolation=cv2.INTER_AREA) cv2.imshow('Skewed resizing', img_scaled)
# Save an image output_file = input_file[:-4] + '_cropped.jpg' cv2.imwrite(output_file, img_cropped) cv2.waitKey()
waitKey()
function displays the images until you hit a key on the keyboard.operating_on_images.py
file that is already provided to you. If you run the code, you will see the following input image:3.133.133.117