smartcrop 可为任意裁剪大小找到合适的图像裁剪区域
smartcrop 可以为任意大小的图像找到合适的裁剪区域。它是一个纯 Go 实现,基于 Jonas Wagner 的 smartcrop.js。
图像: https://www.flickr.com/photos/usfwspacific/8182486789 由美国魚类和野生动物部提供,最初在 2014 年 9 月导入时采用 CC-BY-2.0 许可证。
git clone https://github.com/muesli/smartcrop.git cd smartcrop 去构建
package main
import (
"fmt"
"image"
_ "image/png"
"os"
"github.com/muesli/smartcrop"
"github.com/muesli/smartcrop/nfnt"
)
func main() {
f, _ := os.Open("image.png")
img, _, _ := image.Decode(f)
analyzer := smartcrop.NewAnalyzer(nfnt.NewDefaultResizer())
topCrop, _ := analyzer.FindBestCrop(img, 250, 250)
// The crop will have the requested aspect ratio, but you need to copy/scale it yourself
fmt.Printf("Top crop: %+v\n", topCrop)
type SubImager interface {
SubImage(r image.Rectangle) image.Image
}
croppedimg := img.(SubImager).SubImage(topCrop)
// ...
}还可以查看 smartcrop_test.go 中的测试用例以及 cmd/smartcrop/ 中的 CLI 应用程序,以获得更多工作示例。
前往安装 GitHub.com/muesli/smartcrop/cmd/smartcrop smartcrop 的使用: -height int 作物高度 -输入字符串 输入文件名 -输出字符串 输出文件名 -质量 int jpeg 画质 (默认为 85) -resize 裁剪后重新调整大小 (默认为 true) -width int 裁剪宽度 示例: smartcrop
暂无开放 Issues,或尚未同步最近议题。