func crop(filename string) { f, err := os.Open("testdata/" + filename + ".gif") if err != nil { log.Fatal(err.Error()) } defer f.Close() im, err := gif.DecodeAll(f) if err != nil { log.Fatal(err.Error()) } firstFrame := im.Image[0] imgBounds := firstFrame.Bounds() img := image.NewRGBA(imgBounds) for index, frame := range im.Image { bounds := frame.Bounds() draw.Draw(img, bounds, frame, bounds.Min, draw.Src) im.Image[index] = ImageToPaletted(img.SubImage(image.Rect( imgBounds.Min.X, imgBounds.Min.Y+imgBounds.Dy()/4, imgBounds.Max.X, imgBounds.Max.Y-imgBounds.Dy()/4))) } out, err := os.Create(filename + ".out.gif") if err != nil { log.Fatal(err.Error()) } defer out.Close() gogif.EncodeAll(out, im) }
func process(filename string) { f, err := os.Open("testdata/" + filename + ".gif") if err != nil { log.Fatal(err.Error()) } defer f.Close() im, err := gif.DecodeAll(f) if err != nil { log.Fatal(err.Error()) } firstFrame := im.Image[0] img := image.NewRGBA(firstFrame.Bounds()) // Frames in an animated gif aren't necessarily the same size. Subsequent // frames are overlayed on previous frames. Resizing the frames individually // causes problems due to aliasing of transparent pixels. In theory we can get // around this by building up the resultant frames from all previous frames. // This results in slower processing times and a bigger end image, but so far // I haven't thought of an alternative method. for index, frame := range im.Image { bounds := frame.Bounds() draw.Draw(img, bounds, frame, bounds.Min, draw.Src) im.Image[index] = ImageToPaletted(ProcessImage(img)) } out, err := os.Create(filename + ".out.gif") if err != nil { log.Fatal(err.Error()) } defer out.Close() gogif.EncodeAll(out, im) }