Skip to content

Instantly share code, notes, and snippets.

@tenntenn
Last active December 26, 2018 00:01
Show Gist options
  • Save tenntenn/df953069f2fbcee02df3 to your computer and use it in GitHub Desktop.
Save tenntenn/df953069f2fbcee02df3 to your computer and use it in GitHub Desktop.
Go言語でグレースケールの画像に変換 ref: https://qiita.com/tenntenn/items/0471e5f494df82c3e825
package main
import (
"image"
"image/color"
"image/png"
"os"
)
func main() {
img, _ := png.Decode(os.Stdin)
bounds := img.Bounds()
dest := image.NewGray16(bounds)
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
c := color.Gray16Model.Convert(img.At(x, y))
gray, _ := c.(color.Gray16)
dest.Set(x, y, gray)
}
}
png.Encode(os.Stdout, dest)
}
$ go run gray.go < gopher.png > gopher_gray.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment