Go语言快速制作动图

Go语言快速制作动图,第1张

先看效果:

好像也没谁用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)
	// 创建一个gif
	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)
		// 转成可以用作GIF的图
		img3 := neuquant.Paletted(img2)
		// 插入GIF的一帧
		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)
}

然后就可以把左边的原图变成右边的动图了。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/994851.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-21
下一篇2022-05-21

发表评论

登录后才能评论

评论列表(0条)

    保存