英文:
How to convert image.RGBA (image.Image) to image.Paletted?
问题
我正在尝试从一系列任意的非调色板图像创建一个动画GIF。为了创建一个调色板图像,我需要想出一种调色板的方法。
// 从其他地方获取RGBA等图像
var frames []image.Image
outGif := &gif.GIF{}
for _, simage := range frames {
// TODO: 将图像转换为调色板图像
// bounds := simage.Bounds()
// palettedImage := image.NewPaletted(bounds, ...)
// 将新帧添加到动画GIF中
outGif.Image = append(outGif.Image, palettedImage)
outGif.Delay = append(outGif.Delay, 0)
}
gif.EncodeAll(w, outGif)
在golang标准库中是否有一种简单的方法来实现这个?
英文:
I'm trying to create an animated GIF from a series of arbitrary non-paletted images. In order to create a paletted image, I need to come up with a palette somehow.
// RGBA, etc. images from somewhere else
var frames []image.Image
outGif := &gif.GIF{}
for _, simage := range frames {
// TODO: Convert image to paletted image
// bounds := simage.Bounds()
// palettedImage := image.NewPaletted(bounds, ...)
// Add new frame to animated GIF
outGif.Image = append(outGif.Image, palettedImage)
outGif.Delay = append(outGif.Delay, 0)
}
gif.EncodeAll(w, outGif)
Is there an easy way in golang stdlib to accomplish this?
答案1
得分: 8
似乎在golang的标准库中缺少智能生成调色板的自动方式(如果我理解错了,请纠正我)。但是似乎有一个提供自定义Quantizer
的存根,这让我想到了gogif
项目(这个项目似乎是image.Gif
的来源)。
我从该项目中借用了MedianCutQuantizer
,在这里定义:
https://github.com/andybons/gogif/blob/master/mediancut.go
代码如下:
var subimages []image.Image // 来自其他地方的RGBA等图像
outGif := &gif.GIF{}
for _, simage := range subimages {
bounds := simage.Bounds()
palettedImage := image.NewPaletted(bounds, nil)
quantizer := gogif.MedianCutQuantizer{NumColor: 64}
quantizer.Quantize(palettedImage, bounds, simage, image.ZP)
// 将新帧添加到动画GIF中
outGif.Image = append(outGif.Image, palettedImage)
outGif.Delay = append(outGif.Delay, 0)
}
gif.EncodeAll(w, outGif)
这段代码将生成一个带有调色板的动画GIF。
英文:
It seems an automatic way of intelligently generating palettes is missing from the golang stdlib (correct me if I'm wrong here). But there seems to be a stub for providing your own Quantizer
, which led me to the gogif
project. (Which was the apparent source of image.Gif
.)
I was able to borrow the MedianCutQuantizer
from that project, defined here:
https://github.com/andybons/gogif/blob/master/mediancut.go
Which results in the following:
var subimages []image.Image // RGBA, etc. images from somewhere else
outGif := &gif.GIF{}
for _, simage := range subimages {
bounds := simage.Bounds()
palettedImage := image.NewPaletted(bounds, nil)
quantizer := gogif.MedianCutQuantizer{NumColor: 64}
quantizer.Quantize(palettedImage, bounds, simage, image.ZP)
// Add new frame to animated GIF
outGif.Image = append(outGif.Image, palettedImage)
outGif.Delay = append(outGif.Delay, 0)
}
gif.EncodeAll(w, outGif)
答案2
得分: 7
你可以使用预定义的调色板之一,而不是生成自己的调色板。在这里可以找到预定义的调色板:https://golang.org/pkg/image/color/palette/
...
palettedImage := image.NewPaletted(bounds, palette.Plan9)
draw.Draw(palettedImage, palettedImage.Rect, simage, bounds.Min, draw.Over)
...
英文:
Instead of generating your own palette, you can also use on of the predefined (https://golang.org/pkg/image/color/palette/)
...
palettedImage := image.NewPaletted(bounds, palette.Plan9)
draw.Draw(palettedImage, palettedImage.Rect, simage, bounds.Min, draw.Over)
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论