Image cropping suggestions
The image cropping suggestion feature returns the suggested cropping frame of an image, together with the aesthetic score of each frame, based on the cropping ratios that you specify. Call the DetectImageCropping operation to obtain one cropping frame for each ratio that you specify.
Considerations
The image cropping suggestion feature returns only cropping frame information. DetectImageCropping does not crop the image that is stored at the source address.
The following constraints apply when a cropping start point and cropping dimensions are specified for the source image:
Start point out of bounds — If the X coordinate or the Y coordinate of the specified start point exceeds that of the source image, the
BadRequesterror is returned with the error messageAdvance cut's position is out of image.Frame larger than the source image — If the width and height that are specified from the start point exceed those of the source image, the image is cropped to the boundaries of the source image.
Use cases
Use the cropping frames that the image cropping suggestion feature returns in the following scenarios:
Social media and personal use — Adapt images to the standards of various social platforms, and create personalized profile pictures and backgrounds.
Printing and advertising — Support advertising design, production of promotional materials, and size adjustment for print media.
Security and authentication — Crop photos for identity documents, such as photos that meet the photo specifications of driver's licenses and passports.
Prerequisites
-
You have created an AccessKey pair. For more information, see Create an AccessKey pair.
-
You have activated OSS, created a bucket, and uploaded files to the bucket. For more information, see Upload files.
-
You have activated IMM. For more information, see Activate IMM.
-
You have created a project in the IMM console. For more information, see Create a project.
Note-
You can also call the CreateProject operation to create a project. For more information, see Create a project.
-
You can call the ListProjects operation to list all projects in a specified region.
-
Usage
Call the DetectImageCropping operation to obtain the cropping frame that delivers a good visual effect for the target image ratio. The following sections walk through one complete example: the sample input, the request, the response, the cropping frames that the response contains, and the sample code that makes the call.
Sample input
Intelligent Media Management (IMM) project name:
test-projectObject Storage Service (OSS) storage address of the source image:
oss://test-bucket/test-object.jpgCropping ratios:
1:1,16:9, andauto(automatically set)Sample image:

Sample request
The following sample request specifies the project, the source image, and the cropping ratios:
{
"ProjectName": "test-project",
"SourceURI": "oss://test-bucket/test-object.jpg",
"AspectRatios": "[\"1:1\",\"16:9\",\"auto\"]"
}Sample response
The following response is returned:
{
"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 describes the cropping frame that the sample response returns for each cropping ratio. The Confidence field carries the aesthetic score of the frame. The X coordinate and the Y coordinate specify the cropping start point, with the upper-left corner of the image as the origin.
| Cropping ratio | Aesthetic score (Confidence) | Image width | Image height | X coordinate | Y coordinate |
1:1 | 0.481 | 200 px | 200 px | 14 px | 39 px |
16:9 | 0.748 | 355 px | 200 px | 24 px | 39 px |
auto | 0.844 | 366 px | 250 px | 18 px | 13 px |
Sample code
The following sample code shows how to obtain image cropping suggestions by using IMM SDK for Python.
# -*- 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 account client with an AccessKey ID and an 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 endpoint 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 of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a RAM user to make API calls or perform routine O&M.
# Do not save your AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources in your account may be compromised.
# In this example, the AccessKey pair is read from environment variables to authenticate 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:
# If you copy and run the code, print the response of the API operation as needed.
client.detect_image_cropping_with_options(detect_image_cropping_request, runtime)
except Exception as error:
# Print the error message as needed.
UtilClient.assert_as_string(error.message)
@staticmethod
async def main_async(
args: List[str],
) -> None:
# An AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a RAM user to make API calls or perform routine O&M.
# Do not save your AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources in your account may be compromised.
# In this example, the AccessKey pair is read from environment variables to authenticate 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:
# If you copy and run the code, print the response of the API operation as needed.
await client.detect_image_cropping_with_options_async(detect_image_cropping_request, runtime)
except Exception as error:
# Print the error message as needed.
UtilClient.assert_as_string(error.message)
if __name__ == '__main__':
Sample.main(sys.argv[1:])