Intelligent cropping
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 |
|
|
|
|
|
|
|
|
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"])
For complete instructions on request signing, SDK examples (Java, Python, and Go), and online debugging, refer to the intelligent cropping API documentation in the Alibaba Cloud OpenAPI Developer Portal.
Request parameters
|
Parameter |
Type |
Required |
Description |
|
|
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. |
|
|
Number |
Yes |
The desired output width in pixels. The value must be between 100 and 5000. |
|
|
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 |
|
|
String |
The business status code. A value of |
|
|
String |
The unique identifier for the request. |
|
|
String |
The response message. A value of |
|
|
Boolean |
Indicates whether the call was successful. |
|
|
Object |
The result data object. |
|
|
String |
The URL of the cropped image (an OSS signed URL). |
|
|
Number |
The width of the output image in pixels, which matches |
|
|
Number |
The height of the output image in pixels, which matches |
|
|
Object |
The usage details. |
|
|
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 |
|
|
The image format, resolution, or size is out of range; |
|
|
The input image triggered content moderation. |
|
|
The image download fails or a downstream vision model is temporarily unavailable. Retry with exponential backoff. |
|
|
Your free trial quota is exhausted. You need to purchase a resource plan or disable the 'stop when quota is exhausted' setting. |
|
|
An internal server error occurred. Please save the |
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
ProcessedImageCountfield. For details, refer to Asset Optimization billing.
FAQ
Next steps
-
intelligent poster generation: Orchestrate cropping with other visual capabilities into a single asynchronous task.
-
intelligent matting: Use matting to extract the subject before cropping for a tighter composition.





