Cropping and Pasting
Function Description
Crops the input image, pastes it to the background image, and outputs it to the Image object.
For details about the API, see crop_paste.
API Calling Process
Before calling the image cropping and pasting API, prepare the image object to be cropped and pasted.
Figure 1 Process of calling image processing (cropping and pasting) API


Key steps are demonstrated as follows:
- Perform global initialization by calling mx_init().
- Initialize ImageProcessor.
Construct the ImageProcessor object. You need to specify the device ID during the construction.
- 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 pasted. Subsequently, the image can be processed by using the image processing APIs to generate a final Image object to be cropped and pasted.
- Construct the image cropping and pasting parameters and the output Image.
- Set the matrix of the cropped image and the matrix where the image is to be pasted based on the service requirements. If the sizes of the two matrices are different, the image is automatically resized.
- Construct the background image of the output image. Use the Image constructor to construct the pasted image or other non-empty image as the output.
- Crop the input image and paste it to the specified position by calling crop_paste.
- 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, Image def process(): # Image decoding # Initialize the ImageProcessor object. imageProcessor = ImageProcessor(device_id) image_path = "image_data/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) # Crop and paste the image. paste_image is the background image of the output image, which needs to be constructed by yourself. crop_paste_para = (Rect(300, 100, 550, 350), Rect(100, 100, 1500, 1500)) imageProcessor.crop_paste(decoded_image , crop_paste_para, paste_image) if __name__ == "__main__": base.mx_init() # Initialize resources. process() base.mx_deinit() # Deinitialize resources. |
Parent topic: Media Data Processing