Del*_*tor 4 image-processing go
我目前正在将一个非常基本的图库应用程序从PHP移植到Go.此应用程序具有自动生成缩略图和每个图像的中型版本.
在PHP中,我使用了GD,因为它附带了它并且工作得非常好.(代码在问题的最后).我想我可以在Go中复制它,并go-gd
从https://github.com/bolknote/go-gd找到(再次,代码在最后).它可以工作,但它大约慢10倍(使用测量time wget $URL
).从10 MP图像生成1024x768版本的PHP实现大约需要1秒,而Go-Code需要大约10秒.
有没有什么方法可以加速Go或任何其他图像处理库,Go实现缩放和卷积,同时速度相当快?
public function saveThumb($outName, $options) {
$this->img = imagecreatefromjpeg($filename);
if (!is_dir(dirname($outName))) {
mkdir(dirname($outName), 0777, true);
}
$width = imagesx($this->img);
$height = imagesy($this->img);
if ($options["keep_aspect"]) {
$factor = min($options["size_x"]/$width, $options["size_y"]/$height);
$new_width = round($factor*$width);
$new_height = round($factor*$height);
} else {
$new_width = $options["size_x"];
$new_height = $options["size_y"];
}
// create a new temporary image
$tmp_img = imagecreatetruecolor($new_width, $new_height);
// copy and resize old image into new image
imagecopyresampled($tmp_img, $this->img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
if ($options["sharpen"]) {
// define the sharpen matrix
$sharpen = array(
array(-1, -1.7, -1),
array(-1.7, 20, -1.7),
array(-1, -1.7, -1)
);
// calculate the sharpen divisor
$divisor = array_sum(array_map('array_sum', $sharpen));
// apply the matrix
imageconvolution($tmp_img, $sharpen, $divisor, 0);
}
// save thumbnail into a file
imagejpeg($tmp_img, $outName);
}
Run Code Online (Sandbox Code Playgroud)
func (entry *entry) GenerateThumb(options ImageType, overwrite bool) os.Error {
targetFilename := entry.Filename(imageType)
sourceFilename := entry.Filename(IMAGE_TYPE_FULL)
targetDirname, _ := filepath.Split(targetFilename)
os.MkdirAll(targetDirname, 0777)
targetFi, errT := os.Stat(targetFilename)
sourceFi, errS := os.Stat(sourceFilename)
image := gd.CreateFromJpeg(sourceFilename)
if image == nil {
return os.NewError("Image could not be loaded")
}
var targetX, targetY int = 0, 0
if options.KeepAspect {
factor := math.Fmin(float64(options.SizeX)/float64(image.Sx()), float64(options.SizeY)/float64(image.Sy()))
targetX = int(factor*float64(image.Sx()))
targetY = int(factor*float64(image.Sy()))
} else {
targetX = options.SizeX
targetY = options.SizeY
}
tmpImage := gd.CreateTrueColor(targetX, targetY)
image.CopyResampled(tmpImage, 0, 0, 0, 0, tmpImage.Sx(), tmpImage.Sy(), image.Sx(), image.Sy())
if options.Sharpen {
sharpenMatrix := [3][3]float32{
{-1, -1.7, -1},
{-1.7, 20, -1.7},
{-1, -1.7, -1} }
tmpImage.Convolution(sharpenMatrix, 9.2, 0)
}
tmpImage.Jpeg(targetFilename, 90)
return nil
}
Run Code Online (Sandbox Code Playgroud)
func (entry *entry) GenerateThumb(options ImageType, overwrite bool) os.Error {
targetFilename := entry.Filename(imageType)
sourceFilename := entry.Filename(IMAGE_TYPE_FULL)
targetDirname, _ := filepath.Split(targetFilename)
os.MkdirAll(targetDirname, 0777)
targetFi, errT := os.Stat(targetFilename)
sourceFi, errS := os.Stat(sourceFilename)
if errT == nil && errS == nil {
if targetFi.Mtime_ns > sourceFi.Mtime_ns && !overwrite {
// already up-to-date, nothing to do
return nil
}
}
log.Printf("Generate(\"%v\", %v)\n", imageType, overwrite)
inFile, fErr := os.Open(sourceFilename)
if fErr != nil {
log.Fatal(fErr)
}
defer inFile.Close()
img, _, err := image.Decode(inFile)
if err != nil {
log.Fatal(err)
}
var targetX, targetY int
if options.KeepAspect {
factor := math.Fmin(float64(options.SizeX)/float64(img.Bounds().Max.X), float64(options.SizeY)/float64(img.Bounds().Max.Y))
targetX = int(factor*float64(img.Bounds().Max.X))
targetY = int(factor*float64(img.Bounds().Max.Y))
} else {
targetX = curType.SizeX
targetY = curType.SizeY
}
newImg := resize.Resample(img, image.Rect(0, 0, img.Bounds().Max.X, img.Bounds().Max.Y), targetX, targetY)
var outFile *os.File
outFile, fErr = os.Create(targetFilename)
if fErr != nil {
log.Fatal(fErr)
}
defer outFile.Close()
err = jpeg.Encode(outFile, newImg, &jpeg.Options{90})
if err != nil {
log.Fatal(err)
}
return nil
}
Run Code Online (Sandbox Code Playgroud)