Created
November 23, 2015 13:21
-
-
Save gaoyifan/0329728a6fbdb3d3c7e9 to your computer and use it in GitHub Desktop.
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 ( | |
"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