我正在使用一个Go 包(Go 绑定到 ImageMagick 的 MagickWand C API)到 ImageMagick,我正在从图像中删除边框(裁剪)。我使用修剪功能的方式可以在下面找到。
现在的问题是模糊因素。例如,如果我将值设置为 2000,图像(这里是源)仍然有一些像这样的白色图像:
我创建了一个小 html 来最好地说明问题。它包含两个图像:https ://dl.dropboxusercontent.com/u/15684927/image-trim-problem.html
如您所见,源在右下角有一些像素,这会导致问题。如果我将因子设置为 10000,恐怕我会在其他图片上丢失像素。如果我将其设置为 2000,则在此类图片中修剪不正确。
所以我的实际问题是:“裁剪”/“修剪”图像的最佳方法是什么?
package main
import "gopkg.in/gographics/imagick.v1/imagick"
func main() {
imagick.Initialize()
defer imagick.Terminate()
inputFile := "tshirt-original.jpg"
outputFile := "trimmed.jpg"
mw := imagick.NewMagickWand()
// Schedule cleanup
defer mw.Destroy()
// read image
err := mw.ReadImage(inputFile)
if err != nil {
panic(err)
}
// first trim original image
// fuzz: by default target must match a particular pixel color exactly.
// However, in many cases two colors may differ by a small amount. The fuzz
// member of image defines how much tolerance is acceptable to consider two
// colors as the same. For example, set fuzz to 10 and the color red at
// intensities of 100 and 102 respectively are now interpreted as the same
// color for the purposes of the floodfill.
mw.TrimImage(10000)
// Set the compression quality to 95 (high quality = low compression)
err = mw.SetImageCompressionQuality(95)
if err != nil {
panic(err)
}
// save
err = mw.WriteImage(outputFile)
if err != nil {
panic(err)
}
}