1

I want to create an animated gif with go.

I need for that to compute multiple *image.Paletted.

But when I want to create it, I need p color.Palette but I don't know how to get the palette

How can I have the Palette?

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
Fractale
  • 1,503
  • 3
  • 19
  • 34
  • 1
    You choose the palette for your image. You can only have 256 colors, so you need to choose the ones that best fit your image. If you're not sure, you can start with `pallet.WebSafe` – JimB Jan 17 '17 at 20:14
  • 1
    take a look at related QA [Images lose quality after saving as GIF](http://stackoverflow.com/a/41283985/2521214) and all the linked suff in that answer too ... – Spektre Jan 17 '17 at 20:17
  • thank @JimB it works!!! :D but the picture is ugly compare to png :/ I have test palette.Plan9 for better result but does it have over more precise palette? – Fractale Jan 17 '17 at 20:30
  • 1
    @Fractale: there's no such things as a "more precise palette", because you can only have 256 colors. You can dither the image with the palette you have, calculate a palette that more closely resembles your image, or a combination of both. – JimB Jan 17 '17 at 20:39
  • Thanks @JimB, if I have an image.NRGBA vector how can I compute the best palette for my gif? – Fractale Jan 17 '17 at 20:53
  • 1
    @Fractale: you haven't shown any examples of what you're doing, so you're probably using the built-in FloydSteinberg Drawer for dithering. As for [color quantization](https://en.wikipedia.org/wiki/Color_quantization), a [median-cut](https://en.wikipedia.org/wiki/Median_cut) algorithm is commonly used. – JimB Jan 17 '17 at 22:22
  • Thanks @JimB, I do a raytracer so I have a image.NRGBA vector at the end, now I want to create a gif with a good palette. So how to get a good palette from a image.NRGBA? :) – Fractale Jan 17 '17 at 22:32
  • I already linked to explanations of color quantization and the Heckbert median-cut algorithm. A quick search of godoc shows an implementation [here](https://godoc.org/github.com/soniakeys/quant/median) if you want to try that. – JimB Jan 18 '17 at 16:28
  • In my link is the info too btw the standard VGA palette is already optimized for dithering so you either need to enable dithering in your framework/lib or whatever or do it your self in case you cant... As you doing ray-tracer I bet you want to save output in real time. Good quantization takes time so I would go for dithering which is much much faster. – Spektre Jan 19 '17 at 08:05

1 Answers1

1

The standard library has a own package for that: https://golang.org/pkg/image/color/palette/

At the moment there are two predefined palettes availiable: Plan9 and WebSafe

You can use the package like that:

frame := image.NewPaletted(
    image.Rect(0, 0, 100, 200),
    palette.WebSafe,
)
apxp
  • 5,240
  • 4
  • 23
  • 43