Skip to content

Instantly share code, notes, and snippets.

@gaoyifan
Created November 23, 2015 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaoyifan/0329728a6fbdb3d3c7e9 to your computer and use it in GitHub Desktop.
Save gaoyifan/0329728a6fbdb3d3c7e9 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"encoding/binary"
"fmt"
"image/png"
"os"
)
func main() {
filePng, err := os.Open(os.Args[1])
if err != nil {
fmt.Println(err)
}
defer filePng.Close()
img, err := png.Decode(filePng)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fileBin, err := os.Create(os.Args[2])
if err != nil {
fmt.Println(err)
}
defer fileBin.Close()
MaxX := img.Bounds().Max.X
MaxY := img.Bounds().Max.Y
fmt.Println(MaxX)
fmt.Println(MaxY)
var w bytes.Buffer
for i := 0; i < MaxX; i++ {
//fmt.Println(i)
for j := 0; j < MaxY; j++ {
gray, _, _, _ := img.At(i, j).RGBA()
binary.Write(&w, binary.LittleEndian, int16(gray))
}
}
w.WriteTo(fileBin)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment