Image cropping suggestions

更新时间:
复制 MD 格式

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. For more information, see Create an AccessKey pair.

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

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

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

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

    • You can call the ListProjects operation to list information about all projects created in a specific region.

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.

Image clipping information

  • 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:
        """
        Initialize the client with an AccessKey ID & AccessKey secret.
        @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 domain name that you want to access.
        config.endpoint = f'imm.cn-beijing.aliyuncs.com'
        return imm20200930Client(config)

    @staticmethod
    def main(
        args: List[str],
    ) -> None:
        # An AccessKey pair for an Alibaba Cloud account has permissions on all API operations. For security, we recommend that you use a Resource Access Management (RAM) user to make API calls or for routine O&M.
        # Do not hard-code your AccessKey ID and AccessKey secret in your project code. If you do, your AccessKey pair may be leaked, which compromises the security of all your resources.
        # This example shows how to read an AccessKey pair from environment variables to verify identity for API access. For more information about how to configure environment variables, see https://help.aliyun.com/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:
            # When you run the copied code, print the return value of the API operation.
            client.detect_image_cropping_with_options(detect_image_cropping_request, runtime)
        except Exception as error:
            # If an error occurs, print the error message.
            UtilClient.assert_as_string(error.message)

    @staticmethod
    async def main_async(
        args: List[str],
    ) -> None:
        # An AccessKey pair for an Alibaba Cloud account has permissions on all API operations. For security, we recommend that you use a RAM user to make API calls or for routine O&M.
        # Do not hard-code your AccessKey ID and AccessKey secret in your project code. If you do, your AccessKey pair may be leaked, which compromises the security of all your resources.
        # This example shows how to read an AccessKey pair from environment variables to verify identity for API access. For more information about how to configure environment variables, see https://help.aliyun.com/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:
            # When you run the copied code, print the return value of the API operation.
            await client.detect_image_cropping_with_options_async(detect_image_cropping_request, runtime)
        except Exception as error:
            # If an error occurs, print the error message.
            UtilClient.assert_as_string(error.message)


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