Image Cropping and Resizing

Function Description

Crops and resizes an input image, and outputs the image to the Image object.

For details about the API, see crop_resize.

API Call Process

Before calling the image cropping and resizing APIs, prepare the image object to be cropped and resized.

Figure 1 Process of calling image processing (cropping and resizing) APIs

Key steps are demonstrated as follows:

  1. Perform global initialization by calling mx_init().
  2. Initialize ImageProcessor.

    Construct the ImageProcessor object. You need to specify the device ID during the construction.

  3. Call the image decoding API to decode the input image.

    Decode the image based on services to generate an Image object that can be cropped and resized. Subsequently, the image can be processed by using the image processing APIs to generate a final Image object to be cropped and resized.

  4. Construct the cropping and resizing parameters and the output Image.

    Based on the service requirements, select different modes to construct the input Rect and Size: single-input single-cropping single-resizing or single-input multi-cropping multi-resizing.

  5. Crop and resize the input image by calling the crop_resize API.
  6. Perform deinitialization by calling mx_deinit().

Sample Code

The following is a code example of key steps of functions and features, which is for reference only and cannot be directly copied for execution.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
from mindx.sdk import base  
from mindx.sdk.base import ImageProcessor, Rect, Size, Image

def process():
    # Image decoding
    # Initialize the ImageProcessor object.
    imageProcessor = ImageProcessor(device_id)  
    image_path = "test_image.jpg"
    # Read the image path for decoding. The decoding format is nv12 (YUV_SP_420).
    decoded_image = imageProcessor.decode(image_path, base.nv12)  
    
    #  Image cropping and resizing
    crop_resize_para = [(Rect(300, 100, 550, 350), Size(100, 100))]  
    crop_resize_image = imageProcessor.crop_resize(decoded_image, crop_resize_para)

if __name__ == "__main__":
    base.mx_init()    # Initialize resources.
    process()
    base.mx_deinit()  # Deinitialize resources.