图像智能裁剪

最近更新时间:2023-12-01 17:57:56

我的收藏

简介

本文档提供关于图像智能裁剪的相关 API 概览以及 SDK 示例代码。
API
操作描述
腾讯云数据万象通过 AIImageCrop 接口对图像进行智能裁剪,支持持久化、云上处理及下载时处理。

图像智能裁剪

腾讯云数据万象通过 AIImageCrop 接口对图像进行智能裁剪,支持持久化、云上处理及下载时处理。

方法原型

func (s *CIService) GetAIImageCrop(ctx context.Context, name string, opt *AIImageCropOptions) (*Response, error)

请求示例

// 将 examplebucket-1250000000 和 COS_REGION 修改为真实的信息
bu, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")
b := &cos.BaseURL{BucketURL: bu}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("SECRETID"),
SecretKey: os.Getenv("SECRETKEY"),
}
})
obj := "pic/walk.jpeg"
localPath := "test.jpeg"
opt := &cos.AIImageCropOptions{
Width: 128,
Height: 96,
}
resp, err := c.CI.GetAIImageCrop(context.Background(), obj, opt)
log_status(err)
if err == nil {
fd, _ := os.OpenFile(localPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
io.Copy(fd, resp.Body)
fd.Close()
}

参数说明

type AIImageCropOptions struct {
DetectUrl string `url:"detect-url,omitempty"`
Width int `url:"width,omitempty"`
Height int `url:"height,omitempty"`
Fixed int `url:"fixed,omitempty"`
IgnoreError int `url:"ignore-error,omitempty"`
}
参数名称
参数描述
类型
是否必填
DetectUrl
您可以通过填写 detect-url 处理任意公网可访问的图片链接。不填写 detect-url 时,后台会默认处理 name ,填写了 detect-url 时,后台会处理 detect-url 链接。
string
Width
需要裁剪区域的宽度,与 height 共同组成所需裁剪的图片宽高比例;输入数字请大于0、小于图片宽度的像素值
int
Height
需要裁剪区域的高度,与 width 共同组成所需裁剪的图片宽高比例;输入数字请大于0、小于图片高度的像素值;width : height 建议取值在[1, 2.5]之间,超过这个范围可能会影响效果
int
Fixed
是否严格按照 width 和 height 的值进行输出。取值为0时,宽高比例(width : height)会简化为最简分数,即如果 width 输入10、height 输入20,会简化为1:2;取值为1时,输出图片的宽度等于 width,高度等于 height;默认值为0
int
IgnoreError
当此参数为1时,针对文件过大等导致处理失败的场景,会直接返回原图而不报错
int

结果说明

处理后的图片直接返回至响应体。