func MakeOneGIF(sortedImages []image.Image, delay int, withTransparency bool) (*gif.GIF, error) {
	outGif := &gif.GIF{}

	quantizer := quantize.MedianCutQuantizer{
		Aggregation:    quantize.Mean,
		Weighting:      nil,
		AddTransparent: withTransparency,
	}

	pal := quantizer.QuantizeMultiple(make([]color.Color, 0, 256), sortedImages)

	// Convert each image to a frame in our animated gif
	palettedImages := make(chan orderedPaletted)
	semaphore := make(chan struct{}, runtime.NumCPU())

	// This is surprisingly slow and so is worth parallelizing.
	go func() {
		for k, img := range sortedImages {
			semaphore <- struct{}{}

			go func(k int, img image.Image) {
				defer func() { <-semaphore }()

				palettedImage := image.NewPaletted(img.Bounds(), pal)
				draw.Draw(palettedImage, img.Bounds(), img, image.Point{}, draw.Over)

				palettedImages <- orderedPaletted{
					key:   k,
					image: palettedImage,
				}
			}(k, img)
		}
	}()

	// Save the outputs - in order
	sortedPalattedImages := make([]*image.Paletted, len(sortedImages))
	for range sortedImages {

		palettedImage := <-palettedImages
		sortedPalattedImages[palettedImage.key] = palettedImage.image

	}

	// Assemble into a gif
	for _, palettedImage := range sortedPalattedImages {
		outGif.Image = append(outGif.Image, palettedImage)
		outGif.Delay = append(outGif.Delay, delay)
	}

	return outGif, nil
}
// MakeOneGIF creates an animated gif from an ordered slice of images. The delay
// between frames is in hundredths of a second. The color quantizer is built
// from *all* input images, and the quantized palette is shared across all of
// the output frames.
MakeOneGIF creates an animated gif from an ordered slice of images. The delay
between frames is in hundredths of a second. The color quantizer is built
from *all* input images, and the quantized palette is shared across all of
the output frames.
// MakeOneGIF creates an animated gif from an ordered slice of images . The delay // between frames is in hundredths of a second . The color quantizer is built // from * all * input images and the quantized palette is shared across all of // the output frames .
func MakeOneGIF(sortedImages []image.Image, delay int, withTransparency bool) (*gif.GIF, error) {
	outGif := &gif.GIF{}
	quantizer := quantize.MedianCutQuantizer{
		Aggregation:    quantize.Mean,
		Weighting:      nil,
		AddTransparent: withTransparency,
	}
	pal := quantizer.QuantizeMultiple(make([]color.Color, 0, 256), sortedImages)
	palettedImages := make(chan orderedPaletted)
	semaphore := make(chan struct{}, runtime.NumCPU())
	go func() {
		for k, img := range sortedImages {
			semaphore <- struct{}{}
			go func(k int, img image.Image) {
				defer func() { <-semaphore }()
				palettedImage := image.NewPaletted(img.Bounds(), pal)
				draw.Draw(palettedImage, img.Bounds(), img, image.Point{}, draw.Over)
				palettedImages <- orderedPaletted{
					key:   k,
					image: palettedImage,
				}
			}(k, img)
		}
	}()
	sortedPalattedImages := make([]*image.Paletted, len(sortedImages))
	for range sortedImages {
		palettedImage := <-palettedImages
		sortedPalattedImages[palettedImage.key] = palettedImage.image
	}
	for _, palettedImage := range sortedPalattedImages {
		outGif.Image = append(outGif.Image, palettedImage)
		outGif.Delay = append(outGif.Delay, delay)
	}
	return outGif, nil
}
func MakeOneGIF ( sortedImages [ ] image . Image , delay int , withTransparency bool ) ( * gif . GIF , error ) { outGif := & gif . GIF { } quantizer := quantize . MedianCutQuantizer { Aggregation : quantize . Mean , Weighting : nil , AddTransparent : withTransparency , } pal := quantizer . QuantizeMultiple ( make ( [ ] color . Color , 0 , 256 ) , sortedImages ) palettedImages := make ( chan orderedPaletted ) semaphore := make ( chan struct { } , runtime . NumCPU ( ) ) go func ( ) { for k , img := range sortedImages { semaphore <- struct { } { } go func ( k int , img image . Image ) { defer func ( ) { <- semaphore } ( ) palettedImage := image . NewPaletted ( img . Bounds ( ) , pal ) draw . Draw ( palettedImage , img . Bounds ( ) , img , image . Point { } , draw . Over ) palettedImages <- orderedPaletted { key : k , image : palettedImage , } } ( k , img ) } } ( ) sortedPalattedImages := make ( [ ] * image . Paletted , len ( sortedImages ) ) for range sortedImages { palettedImage := <- palettedImages sortedPalattedImages [ palettedImage . key ] = palettedImage . image } for _ , palettedImage := range sortedPalattedImages { outGif . Image = append ( outGif . Image , palettedImage ) outGif . Delay = append ( outGif . Delay , delay ) } return outGif , nil }
MakeOneGIF creates an animated gif from an ordered slice of images.
MakeOneGIF creates an animated gif from an ordered slice of images .
// Convert each image to a frame in our animated gif // This is surprisingly slow and so is worth parallelizing. // Save the outputs - in order // Assemble into a gif