All Products
Search
Document Center

Intelligent Media Management:Image cropping suggestion

Last Updated:Nov 14, 2024

The image cropping suggestion feature can return the suggested cropping frame for an image and the aesthetic score of the cropping solution based on a specified ratio. If you specify multiple cropping ratios for an image, the DetectImageCropping operation provides suggestions for cropping the image based on these ratios. This topic describes how to use the image cropping suggestion feature.

Scenarios

  • Social media and personal use: adaptation to various social platform standards and creation of personalized profile pictures and backgrounds.

  • Printing and advertising: advertising design, production of promotional materials, and resizing for print media.

  • Security and authentication: cropping of identity document images, such as specification adaptation of driver licenses and passport images.

Note

The image cropping suggestion feature returns only the cropping frame information and does not crop images stored in the source address.

Prerequisites

  • An AccessKey pair is created and obtained. For more information, see Create an AccessKey pair.

  • OSS is activated, a bucket is created, and objects are uploaded to the bucket. For more information, see Upload objects.

  • IMM is activated. For more information, see Activate IMM.

  • A project is created in the IMM console. For more information, see Create a project.

    Note
    • You can call the CreateProject operation to create a project. For more information, see CreateProject.

    • You can call the ListProjects operation to query the existing projects in a specific region. For more information, see ListProjects.

Usage

Call the DetectImageCropping operation to obtain the cropping frame with a good visual effect under the specified image ratio. For more information, see DetectImageCropping.

Example

  • Intelligent Media Management (IMM) project: test-project

  • Image location: oss://test-bucket/test-object.jpg

  • Cropping ratios: 1:1, 16:9, and auto (automatically set)

  • Image:

    test-object

Sample request

{
    "ProjectName": "test-project",
    "SourceURI": "oss://test-bucket/test-object.jpg",
    "AspectRatios": "[\"1:1\",\"16:9\", \"auto\"]"
}

Sample response

{
  "RequestId": "AFD39290-659F-5474-AFF5-5640CB140405",
  "Croppings": [
    {
      "AspectRatio": "1:1",
      "Confidence": 0.481,
      "Boundary": {
        "Left": 14,
        "Top": 39,
        "Height": 200,
        "Width": 200
      }
    },
    {
      "AspectRatio": "16:9",
      "Confidence": 0.748,
      "Boundary": {
        "Left": 24,
        "Top": 39,
        "Height": 200,
        "Width": 355
      }
    },
    {
      "AspectRatio": "auto",
      "Confidence": 0.844,
      "Boundary": {
        "Left": 18,
        "Top": 13,
        "Height": 250,
        "Width": 366
      }
    }
  ]
}

Cropping suggestions

The following table provides suggestions on image cropping based on the sample response.

Cropping ratio

Parameter

1:1

  • Image height: 200 px

  • Image width: 200 px

  • X coordinate of the area that you want to crop (The default value is the X coordinate of the upper-left corner of the image): 14 px

  • Y coordinate of the area that you want to crop (The default value is the Y coordinate of the upper-left corner of the image): 39 px

16:9

  • Image height: 200 px

  • Image width: 355 px

  • X coordinate of the area that you want to crop (The default value is the X coordinate of the upper-left corner of the image): 24 px

  • Y coordinate of the area that you want to crop (The default value is the Y coordinate of the upper-left corner of the image): 39 px

auto (automatically set)

  • Image height: 250 px

  • Image width: 366 px

  • X coordinate of the area that you want to crop (The default value is the X coordinate of the upper-left corner of the image):18 px

  • Y coordinate of the area that you want to crop (The default value is the Y coordinate of the upper-left corner of the image): 13 px

Important
  • If the specified starting X coordinate and Y coordinate exceed those of the source image, the BadRequest error and the "Advance cut's position is out of image." error message are returned.

  • If the width and height specified from the starting point exceed those of the source image, the source image is cropped to the boundaries.

Sample code

The following sample code provides an example on how to use IMM SDK for Python to crop images:

# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import sys
import os
from typing import List

from alibabacloud_imm20200930.client import Client as imm20200930Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_imm20200930 import models as imm_20200930_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient


class Sample:
    def __init__(self):
        pass

    @staticmethod
    def create_client(
        access_key_id: str,
        access_key_secret: str,
    ) -> imm20200930Client:
        """
        Use your AccessKey ID and AccessKey secret to initialize the client. 
        @param access_key_id:
        @param access_key_secret:
        @return: Client
        @throws Exception
        """
        config = open_api_models.Config(
            access_key_id=access_key_id,
            access_key_secret=access_key_secret
        )
        # Specify the IMM endpoint. 
        config.endpoint = f'imm.cn-beijing.aliyuncs.com'
        return imm20200930Client(config)

    @staticmethod
    def main(
        args: List[str],
    ) -> None:
        # The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To prevent security risks, we recommend that you call API operations or perform routine O&M as a Resource Access Management (RAM) user. 
        # We recommend that you do not include your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised. 
        # In this example, the AccessKey pair is read from the environment variables to implement identity verification for API access. For more information about how to configure environment variables, see https://www.alibabacloud.com/help/document_detail/2361894.html. 
        imm_access_key_id = os.getenv("AccessKeyId")
        imm_access_key_secret = os.getenv("AccessKeySecret")
        # Initialize the client. 
        client = Sample.create_client(imm_access_key_id, imm_access_key_secret)
        detect_image_cropping_request = imm_20200930_models.DetectImageCroppingRequest(
            project_name='test-project',
            source_uri='oss://test-bucket/test-object.jpg',
            aspect_ratios='["1:1","16:9","auto"]'
        )
        runtime = util_models.RuntimeOptions()
        try:
            # You can choose to print the response of the API operation. 
            client.detect_image_cropping_with_options(detect_image_cropping_request, runtime)
        except Exception as error:
            # Print the error message if necessary. 
            UtilClient.assert_as_string(error.message)

    @staticmethod
    async def main_async(
        args: List[str],
    ) -> None:
        # The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To prevent security risks, we recommend that you call API operations or perform routine O&M as a RAM user. 
        # We recommend that you do not include your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised. 
        # In this example, the AccessKey pair is read from the environment variables to implement identity verification for API access. For more information about how to configure environment variables, see https://www.alibabacloud.com/help/document_detail/2361894.html. 
        imm_access_key_id = os.getenv("AccessKeyId")
        imm_access_key_secret = os.getenv("AccessKeySecret")
        # Initialize the client. 
        client = Sample.create_client(imm_access_key_id, imm_access_key_secret)
        detect_image_cropping_request = imm_20200930_models.DetectImageCroppingRequest(
            project_name='test-project',
            source_uri='oss://test-bucket/test-object.jpg',
            aspect_ratios='["1:1","16:9","auto"]'
        )
        runtime = util_models.RuntimeOptions()
        try:
            # You can choose to print the response of the API operation. 
            await client.detect_image_cropping_with_options_async(detect_image_cropping_request, runtime)
        except Exception as error:
            # Print the error message if necessary. 
            UtilClient.assert_as_string(error.message)


if __name__ == '__main__':
    Sample.main(sys.argv[1:])