Skip to content

Instantly share code, notes, and snippets.

@gaoyifan gaoyifan/png2bin.go
Created Nov 23, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.