mirror of
https://github.com/esimov/pigo.git
synced 2025-09-27 04:26:32 +08:00
24 lines
463 B
Go
24 lines
463 B
Go
package pigo
|
|
|
|
import (
|
|
"image"
|
|
)
|
|
|
|
// RgbToGrayscale converts the image to grayscale mode.
|
|
func RgbToGrayscale(src image.Image) []uint8 {
|
|
width, height := src.Bounds().Dx(), src.Bounds().Dy()
|
|
gray := make([]uint8, width*height)
|
|
|
|
for y := 0; y < height; y++ {
|
|
for x := 0; x < width; x++ {
|
|
r, g, b, _ := src.At(x, y).RGBA()
|
|
gray[y*width+x] = uint8(
|
|
(0.299*float64(r) +
|
|
0.587*float64(g) +
|
|
0.114*float64(b)) / 256,
|
|
)
|
|
}
|
|
}
|
|
return gray
|
|
}
|