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)
}
Deprecated: Array and string offset access syntax with curly braces is deprecated in /home/golayrva/public_html/lib/Varien/Filter/Template/Tokenizer/Abstract.php on line 89
Input :
Output:
Most Helpful This Week
Find length of Channel, Pointer, Slice, String and Map
Add N number of Year, Month, Day, Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to current date-time
How to fetch an Integer variable as String in Go?
Dynamic JSON parser without Struct in Golang
How to play and pause execution of goroutine?
How to add and update elements in Map?
Example: How to use TeeReader from IO Package in Golang?
How to get the current date and time with timestamp in local and other timezones ?
Different ways to convert Byte Array into String
How to Unmarshal nested JSON structure?