How to convert Colorful PNG image to Gray-scale?
The Decode function decodes an image that has been encoded in a registered format. RGBA is an in-memory image whose At method returns color.RGBA values.
Example
package main
import (
"image"
"image/color"
"image/png"
"math"
"log"
"os"
)
func main() {
filename := "pokeball.png"
infile, err := os.Open(filename)
if err != nil {
log.Printf("failed opening %s: %s", filename, err)
panic(err.Error())
}
defer infile.Close()
imgSrc, _, err := image.Decode(infile)
if err != nil {
panic(err.Error())
}
// Create a new grayscale image
bounds := imgSrc.Bounds()
w, h := bounds.Max.X, bounds.Max.Y
grayScale := image.NewGray(image.Rectangle{image.Point{0, 0}, image.Point{w, h}})
for x := 0; x < w; x++ {
for y := 0; y < h; y++ {
imageColor := imgSrc.At(x, y)
rr, gg, bb, _ := imageColor.RGBA()
r := math.Pow(float64(rr), 2.2)
g := math.Pow(float64(gg), 2.2)
b := math.Pow(float64(bb), 2.2)
m := math.Pow(0.2125*r+0.7154*g+0.0721*b, 1/2.2)
Y := uint16(m + 0.5)
grayColor := color.Gray{uint8(Y >> 8)}
grayScale.Set(x, y, grayColor)
}
}
// Encode the grayscale image to the new file
newFileName := "grayscale.png"
newfile, err := os.Create(newFileName)
if err != nil {
log.Printf("failed creating %s: %s", newfile, err)
panic(err.Error())
}
defer newfile.Close()
png.Encode(newfile,grayScale)
}
Output:
Most Helpful This Week
How to use function from another file golang?
Regular expression to extract filename from given path in Golang
How to iterate over an Array using for loop?
Dynamic XML parser without Struct in Go
Golang Functions Returning Multiple Values
How to convert Boolean Type to String in Go?
Convert Int data type to Int16 Int32 Int64
Sierpinski triangle in Go Programming Language
How to replace emoji characters in string using regex in Golang?
How to get the current date and time with timestamp in local and other timezones ?