先看效果:
好像也没谁用Golang写图像处理算法的。所以自己写了两个库:
go get github.com/CuteReimu/colortools
go get github.com/CuteReimu/neuquant
使用用法非常简单,代码如下:
package main
import (
"github.com/CuteReimu/colortools"
"github.com/CuteReimu/neuquant"
"image"
"image/color"
"image/gif"
"image/jpeg"
"os"
)
func main() {
f, _ := os.Open("1.jpg")
defer func() { _ = f.Close() }()
img, _ := jpeg.Decode(f)
result := &gif.GIF{}
for j := 0; j < 360; j += 30 {
c := make([]color.Color, 361)
p := make([]float64, 361)
for i := 0; i <= 360; i++ {
c[i] = &colortools.HSV{H: float64(i + j), S: 1.0, V: 0.5}
p[i] = float64(i) / 360.0
}
img1 := colortools.NewLineGradChgColorImage(img.Bounds(), c, p, img.Bounds())
img2 := colortools.Screen(img1, img)
nq, palette := neuquant.AnalyzePalette(img2)
img3 := image.NewPaletted(img2.Bounds(), palette)
neuquant.ParallelWritePalette(nq, img2, img3)
result.Image = append(result.Image, img3)
result.Delay = append(result.Delay, 10)
}
f2, _ := os.Create("1.gif")
defer func() { _ = f2.Close() }()
_ = gif.EncodeAll(f2, result)
}
然后就可以把左边的原图变成右边的动图了。