golang实现合并多张照片成gif动态图片代码

代码语言:golang

所属分类:其他

代码描述:golang实现合并多张照片成gif动态图片代码,不适用第三方库实现。

代码标签: golang 合并 多张 照片 gif 图片 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

package main

import (
    "errors"
    "fmt"
    "golang.org/x/image/bmp"
    "golang.org/x/image/draw"
    "golang.org/x/image/webp"
    "image"
    "image/color/palette"
    "image/gif"
    "image/jpeg"
    "image/png"
    "os"
    "path/filepath"
)

func main() {
    imgArr := []string{"/data/wwwroot/default/asset/testimg3.png","/data/wwwroot/default/asset/testimg2.png","/data/wwwroot/default/asset/testimg1.png"}
    delay:=100//设置延迟
    //新建gif结构体
    outGif := &gif.GIF{}
    for _, img := range imgArr {
        //图像解码
        inGif,err:=imageDecode(img)
        if err!=nil {
            fmt.Println("图像解码失败",err)
            continue
        }
        //缩放
        dst := image.NewRGBA(image.Rect(0, 0, 640, 640))
        resize(dst,inGif)

        bounds := dst.Bounds()
        //创建一个新的image.Paletted对象用于存储带有调色板的图像
        palettedImg := image.NewPaletted(bounds, palette.Plan9)
        //将原始图像绘制到 palettedImg
        draw.Draw(palettedImg, bounds, dst, bounds.Min, draw.Src)
        outGif.Image = append(outGif.Image, palettedImg)
        outGif.Delay = append(outGif.Delay, delay)
    }
    f, _ := o.........完整代码请登录后点击上方下载按钮下载查看

网友评论0