Skip to content

Instantly share code, notes, and snippets.

@prl900
Created March 22, 2019 03:42
Show Gist options
  • Save prl900/000afde2bf5a84d74d6d6333306304bb to your computer and use it in GitHub Desktop.
Save prl900/000afde2bf5a84d74d6d6333306304bb to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"image"
"image/color"
"image/png"
"os"
)
func main() {
imgfile, err := os.Open("/home/prl900/Downloads/red.png")
if err != nil {
fmt.Println("file not found!")
os.Exit(1)
}
img, _ := png.Decode(imgfile)
imgfile.Close()
red := img.(*image.NRGBA64)
gray := image.NewGray16(red.Bounds())
for i := 0; i < red.Bounds().Max.X; i++ {
for j := 0; j < red.Bounds().Max.Y; j++ {
val := red.NRGBA64At(i, j)
if val.R != 32767 {
gray.SetGray16(i, j, color.Gray16{val.R})
}
}
}
out, err := os.Create("/home/prl900/Downloads/gray.png")
err = png.Encode(out, gray)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment