Last active
July 17, 2018 02:00
-
-
Save linw1995/820b05ac3fbab34937635d30a61740fb to your computer and use it in GitHub Desktop.
Golang pkg "image/gif" cookbook.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"image" | |
"image/draw" | |
"image/gif" | |
"image/png" | |
"os" | |
"strings" | |
) | |
func main() { | |
if len(os.Args) == 1 { | |
panic(fmt.Errorf("len(os.Args) less than 2")) | |
} | |
for _, fn := range os.Args[1:] { | |
f, err := os.Open(fn) | |
if err != nil { | |
panic(err) | |
} | |
defer f.Close() | |
inGif, err := gif.DecodeAll(f) | |
if err != nil { | |
panic(err) | |
} | |
config, _ := gif.DecodeConfig(f) | |
rect := image.Rect(0, 0, config.Width, config.Height) | |
if rect.Min == rect.Max { | |
var max image.Point | |
for _, frame := range inGif.Image { | |
maxF := frame.Bounds().Max | |
if max.X < maxF.X { | |
max.X = maxF.X | |
} | |
if max.Y < maxF.Y { | |
max.Y = maxF.Y | |
} | |
} | |
rect.Max = max | |
} | |
format := fmt.Sprintf("%%0%dd", len(string(len(inGif.Image)))+1) | |
for i, srcimg := range inGif.Image { | |
img := image.NewRGBA(rect) | |
subfn := strings.Split(fn, ".")[0] + fmt.Sprintf(format, i) + ".png" | |
f, err := os.Create(subfn) | |
if err != nil { | |
panic(err) | |
} | |
defer f.Close() | |
draw.Draw(img, srcimg.Bounds(), srcimg, srcimg.Rect.Min, draw.Src) | |
fmt.Printf("\r%s", subfn) | |
png.Encode(f, img) | |
} | |
fmt.Println("\r", fn) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"regexp" | |
"image" | |
"image/color" | |
"image/color/palette" | |
"image/draw" | |
"image/gif" | |
_ "image/png" | |
_ "image/jpeg" | |
"io/ioutil" | |
) | |
func main() { | |
if len(os.Args) == 1 { | |
panic(fmt.Errorf("len(os.Args) less than 2")) | |
} | |
format := fmt.Sprintf("%%0%dd", len(string(len(os.Args)))+1) | |
palette := append(palette.WebSafe, color.Transparent) | |
for i, fnFmt := range os.Args[1:] { | |
pattern, err := regexp.Compile(fnFmt) | |
if err != nil { | |
panic(err) | |
} | |
files, _ := ioutil.ReadDir("./") | |
outGif := &gif.GIF{} | |
for _, file := range files { | |
if pattern.MatchString(file.Name()) { | |
fmt.Print("\r", file.Name()) | |
f, err := os.Open(file.Name()) | |
if err != nil { | |
panic(err) | |
} | |
defer f.Close() | |
img, _, err := image.Decode(f) | |
if err != nil { | |
panic(err) | |
} | |
bounds := img.Bounds() | |
palettedImage := image.NewPaletted(bounds, palette) | |
draw.Draw(palettedImage, bounds, img, image.ZP, draw.Src) | |
outGif.Image = append(outGif.Image, palettedImage) | |
outGif.Delay = append(outGif.Delay, 1) | |
// outGif.Disposal = append(outGif.Disposal, gif.DisposalPrevious) | |
} | |
} | |
f, err := os.Create(fmt.Sprintf(format, i) + ".gif") | |
if err != nil { | |
panic(err) | |
} | |
defer f.Close() | |
gif.EncodeAll(f, outGif) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"image" | |
"image/color" | |
"image/color/palette" | |
"image/gif" | |
"math" | |
"os" | |
) | |
type Circle struct { | |
X, Y, R float64 | |
} | |
func (c *Circle) Brightness(x, y float64) uint8 { | |
var dx, dy float64 = c.X - x, c.Y - y | |
d := math.Sqrt(dx*dx+dy*dy) / c.R | |
if d > 1 { | |
return 0 | |
} else { | |
return 255 | |
} | |
} | |
func main() { | |
const ( | |
w, h int = 256, 256 | |
R float64 = 50 | |
frameN int = 30 | |
delay int = 100 / frameN | |
) | |
var palette = append(palette.WebSafe, color.Transparent) | |
var images []*image.Paletted | |
var delays []int | |
var disposals []byte | |
var hw, hh float64 = float64(w / 2), float64(h / 2) | |
circles := []*Circle{&Circle{}, &Circle{}, &Circle{}} | |
for step := 0; step < frameN; step++ { | |
img := image.NewPaletted(image.Rect(0, 0, w, h), palette) | |
images = append(images, img) | |
delays = append(delays, delay) | |
disposals = append(disposals, gif.DisposalPrevious) | |
θ := 2.0 * math.Pi / float64(frameN) * float64(step) | |
for i, circle := range circles { | |
θ0 := 2 * math.Pi / 3 * float64(i) | |
circle.X = hw - 30*math.Sin(θ0) - 30*math.Sin(θ0+θ) | |
circle.Y = hh - 30*math.Cos(θ0) - 30*math.Cos(θ0+θ) | |
circle.R = R | |
} | |
for x := 0; x < w; x++ { | |
for y := 0; y < h; y++ { | |
cr := circles[0].Brightness(float64(x), float64(y)) | |
cg := circles[1].Brightness(float64(x), float64(y)) | |
cb := circles[2].Brightness(float64(x), float64(y)) | |
if cr|cg|cb > 0x00 { | |
img.Set(x, y, color.RGBA{cr, cg, cb, 255}) | |
} else { | |
img.Set(x, y, color.Transparent) | |
} | |
} | |
} | |
} | |
f, err := os.Create("rgb.gif") | |
if err != nil { | |
panic(err) | |
} | |
defer f.Close() | |
gif.EncodeAll(f, &gif.GIF{ | |
Image: images, | |
Delay: delays, | |
Disposal: disposals, | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rgb.go Output:
img2gif.go Input & gif2png.go Output:
gif2png.go Input & img2gif.go Output: