9宫格群头像生成——golang
func imageCompose(imagePath string, localPaths []string, gap int) {
imageSize := 80
imageRow := int(math.Sqrt(float64(len(localPaths)))) // 根据传入图片数量判断行列数量
imageCol := imageRow
rowOffset := gap
colOffset := gap
backImg := imaging.New(imageSize*imageRow+gap*(imageCol+1), imageSize*imageCol+gap*(imageRow+1), color.NRGBA{255, 255, 255, 255})
tempRow, tempCol := 1, 1
for tempRow < imageRow+1 {
for tempCol < imageCol+1 {
imgIndex := imageCol*(tempRow-1) + tempCol - 1
resizeImg, err := imaging.Open(localPaths[imgIndex])
if err != nil {
fmt.Println(err)
}
resizeImg = imaging.Blur(imaging.Resize(resizeImg, imageSize, imageSize, imaging.Lanczos), 2) // 加了模糊操作
backImg = imaging.Paste(backImg, resizeImg, image.Pt((tempCol-1)*imageSize+colOffset, (tempRow-1)*imageSize+rowOffset))
tempCol += 1
colOffset += gap
}
tempRow += 1
rowOffset += gap
tempCol = 1
colOffset = gap
}
err := imaging.Save(backImg, imagePath)
if err != nil {
fmt.Println(err)
}
return
}