# Image Smart Cutting

Interface should be called on the server side, not in the front end (Weixin Mini Program, web pages, APP, etc.) directly called, specific reference interface call guide

Interface name: img AiCrop

This interface is used to intelligently identify and crop the subject area of the picture

# 1. How to call

# HTTPS calls

POST https://api.weixin.qq.com/cv/img/aicrop?access_token=ACCESS_TOKEN&img_url=IMG_URL

# Cloud Calls

  • Call method: img.aiCrop

  • The input and exit parameters are the same as the HTTPS call, which can be called in the cloud call documentation

# Third party invocation

  • This interface supports Third Party Platform generation business call.

  • The permission set to which this interface belongs is id: 117

  • When a service provider is authorized by one of the permissions set, it can call on behalf of the merchant by using authorizer_access_token , which can be viewed in the third-party call documentation.

# 2. Request parameters

# Query parametersQuery String parameters

Parameter NametypeRequired to fill inExampleIntroductions
access_tokenstringyesACCESS_TOCKENInterface invocation credentials, using access_token , authorizer_access_token
img_urlstringnoENCODE_URLTo detect the image url, pass this without passing the img parameter.

# Request BodyRequest Payload

Parameter NametypeRequired to fill inIntroductions
imgformdatanoImage file, less than 2 M
ratiosstringnoThe ratio of width to height; If you provide multiple ratios of width to height, separate them by an English comma, ". Up to 5 ratios are supported

# 3. Return Parameters

# Response Payload

Parameter NametypeIntroductions
errcodenumberError code
errmsgstringError message
resultsobjarraySmart cropping results
img_sizeobjectImage size

# Res.results(Array)Object Payload

Smart cropping results

Parameter NametypeIntroductions
crop_leftnumberUpper left corner x
crop_topnumberUpper left corner y
crop_rightnumberLower right corner x
crop_bottomnumberLower right corner y

# Res.img_sizeObject Payload

Image size

Parameter NametypeIntroductions
wnumberwidth
hnumberheight

# 4. Note

  1. Support the use of img parameter real-time upload, also support the use of img_url parameter transfer picture address, by WeChat background download picture identification.
  2. The ratios parameter is optional, if it is empty, the algorithm will crop the best aspect ratio automatically. If you provide multiple ratios of width to height, separate them by an English comma, ". Up to 5 ratios are supported
  3. File size limit: less than 2 M images

# 5. Code examples

# 5.1 Request to upload a file

Example Requests

curl -F 'img=@test.jpg' -F 'ratios=1,2.35' 'http://api.weixin.qq.com/cv/img/aicrop?access_token=ACCESS_TOCKEN'

Return an example

{
  "errcode": 0,
  "errmsg": "ok",
  "results": [
    {
      "crop_left": 112,
      "crop_top": 0,
      "crop_right": 839,
      "crop_bottom": 727
    },
    {
       "crop_left": 0,
       "crop_top": 205,
       "crop_right": 965,
       "crop_bottom": 615
   }
  ],
  "img_size": {
    "w": 966,
    "h": 728
  }
}

Example Requests

curl -F 'ratios=1,2.35' "http://api.weixin.qq.com/cv/img/aicrop?img_url=ENCODE_URL&access_token=ACCESS_TOCKEN"

Return an example

{
    "errcode": 0, 
    "errmsg": "ok", 
    "results": [ //智能裁剪结果
        {
            "crop_left": 112, 
            "crop_top": 0, 
            "crop_right": 839, 
            "crop_bottom": 727
        }, 
        {
            "crop_left": 0, 
            "crop_top": 205, 
            "crop_right": 965, 
            "crop_bottom": 615
        }
    ], 
    "img_size": { //图片大小
        "w": 966, 
        "h": 728
    }
}

# 6. Error code

The following is a list of error codes for this interface, other error codes can refer to General error codes

Error codeError DescriptionSolutions
-1system errorA system error
40001invalid credential  access_token isinvalid or not latestAppSecret error while getting access_token, or access_token is invalid.Developers should check that AppSecret is correct, or that they are calling interfaces for the appropriate Official Account
101000invalid image urlImage URL error
101002invalid image dataImage data is not valid

# 7. Scope of application

How this interface can be invoked under different account types:
Weixin Mini Program Official Account Service Account
Certification only Certification only
  • ✔: The account can call this interface
  • Authentication only: means that only authenticated accounts are allowed to be invoked by the enterprise entity, and accounts that are not authenticated or do not support authentication cannot be invoked.
  • Other account types that are not expressly stated may not be called on this interface without special instructions;