栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

Go语言快速制作动图

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Go语言快速制作动图

先看效果:

好像也没谁用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)
}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/905959.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号