Intelligent cropping

Updated at:

This feature resizes an image to your specified dimensions. It uses subject detection to accurately crop the image for various use cases, such as multi-platform distribution and ad placements. This API is called synchronously.

Use cases

  • Multi-platform content distribution: Automatically generate content in various sizes to meet the dimension requirements of different platforms.

  • Content delivery: Crop a single source image into multiple sizes for different ad placements. This highlights the main subject, reduces empty space, and emphasizes product features.

Features

  • Automatically detects the main subject of an image for precise cropping.

  • Supports custom output dimensions to adapt to different platforms and devices.

Before and after

Original image

Cropped to 1000 × 1000

Cropped to 3000 × 2000

image.png

image

image.png

image.png

image.png

image.png

API

POST /rest/ai/image/cropping

Quick start

import json

data = json.dumps({
    "ImageUrl": "https://example.com/product.jpg",
    "TargetWidth": 1000,
    "TargetHeight": 1000
})
result = make_signed_request("/rest/ai/image/cropping", data)

d = result["Data"]
print("Cropped image URL:", d["ImageUrl"])
print("Output dimensions:", d["Width"], "x", d["Height"])

Request parameters

Parameter

Type

Required

Description

ImageUrl

String

Yes

The URL of the source image. The image must be in JPG, JPEG, PNG, BMP, or WEBP format. The resolution must be between 256×256 and 3000×3000 pixels. The file size cannot exceed 10 MB.

TargetWidth

Number

Yes

The desired output width in pixels. The value must be between 100 and 5000.

TargetHeight

Number

Yes

The desired output height in pixels. The value must be between 100 and 5000.

If the aspect ratio of the source image differs significantly from the target aspect ratio, the service prioritizes keeping the main subject intact. If necessary, it expands or pads the non-subject areas.

Response fields

Field

Type

Description

Code

String

The business status code. A value of success indicates that the request was successful.

RequestId

String

The unique identifier for the request.

Message

String

The response message. A value of "Success" indicates a successful request.

Success

Boolean

Indicates whether the call was successful.

Data

Object

The result data object.

Data.ImageUrl

String

The URL of the cropped image (an OSS signed URL).

Data.Width

Number

The width of the output image in pixels, which matches TargetWidth.

Data.Height

Number

The height of the output image in pixels, which matches TargetHeight.

Data.UsageMap

Object

The usage details.

Data.UsageMap.ProcessedImageCount

Number

The number of images processed in this call. This field is used for billing reconciliation.

Full example

Request example

{
  "ImageUrl": "https://example.com/source.jpg",
  "TargetWidth": 800,
  "TargetHeight": 800
}

Response example

{
  "Data": {
    "ImageUrl": "https://oss.aliyuncs.com/.../crop-result.png",
    "Height": 800,
    "UsageMap": { "ProcessedImageCount": 1 },
    "Width": 800
  },
  "RequestId": "616BD57A-D7ED-1F8F-85FA-B52C8D752493",
  "Message": "Success",
  "Code": "success",
  "Success": true
}

Best practices

  • Batch distribution: To reuse a single source image across multiple placements, submit separate requests for each desired specification, such as 1000×1000, 750×1000, and 1200×628. Requests can be sent concurrently, up to the QPS limit of 5.

  • Centering the subject: If the main subject in the source image is near the edge, consider using intelligent erasing first to remove distracting elements. This helps ensure the subject is detected correctly before cropping.

  • Handling subject overflow: If the target aspect ratio is vastly different from the source's, for example, cropping a 1:1 image to 16:9, the subject might be cut off. Consider using image expansion to add more background before cropping.

Error codes

Error code

Cause

InvalidParameter

The image format, resolution, or size is out of range; TargetWidth or TargetHeight is not within 100 to 5000; or ImageUrl is empty.

InputContentBlocked

The input image triggered content moderation.

DownstreamUnavailable

The image download fails or a downstream vision model is temporarily unavailable. Retry with exponential backoff.

FreeQuotaExhausted

Your free trial quota is exhausted. You need to purchase a resource plan or disable the 'stop when quota is exhausted' setting.

InternalError

An internal server error occurred. Please save the RequestId and contact technical support.

For a complete list of error codes, see error codes.

Limitations

  • Invocation mode: Synchronous. The default QPS is 5, with a maximum request timeout of 30 seconds.

  • Input image: JPG / JPEG / PNG / BMP / WEBP; resolution from 256×256 to 3000×3000; file size ≤ 10 MB.

  • Output dimensions: Between 100 and 5000 pixels. Width and height can be set independently.

  • Billing: ¥0.001 per image, based on the ProcessedImageCount field. For details, refer to Asset Optimization billing.

FAQ

What is the cropping strategy?

The service automatically detects the main subject in the image and crops the image to the target dimensions while preserving as much of the subject as possible.

Can cropping cut off the main subject?

Yes, it is possible. If the image contains multiple subjects or if the target aspect ratio is very different from the source image's aspect ratio, cropping might cut off parts of the main subject.

Can I specify an area or content to keep?

No. The cropping is based on the detected subject. You cannot specify a particular region or content to preserve.

Next steps