Golang : Grayscale Image
Another feature that I like about this Disintegration Imaging package is the ability to generate grayscaled image. The codes below will demonstrate how to quickly turn an image into grayscale version.
package main
import (
"fmt"
"github.com/disintegration/imaging"
"os"
"runtime"
)
func main() {
// maximize CPU usage for maximum performance
runtime.GOMAXPROCS(runtime.NumCPU())
// load original image
img, err := imaging.Open("./big.jpg")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// grayscale the image
grayimg := imaging.Grayscale(img)
// save grayscaled image
err = imaging.Save(grayimg, "./grayscaled.png")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// everything ok
fmt.Println("Done")
}
big.jpg
grayscaled.png
References :
See also : Golang : Generate thumbnails from images
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+16.7k How to enable MariaDB/MySQL logs ?
+3.9k Mac OSX : Get disk partitions' size, type and name
+32.9k Golang : Proper way to set function argument default value
+28.2k Golang : How to create new XML file ?
+10.1k Golang : Fix - does not implement sort.Interface (missing Len method)
+4.9k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+15.5k Golang : Convert slice to array
+8.5k Golang : Web(Javascript) to server-side websocket example
+8.7k Golang : Convert(cast) string to int64
+5k Golang : Struct field tags and what is their purpose?
+19.4k Golang : Pipe output from one os.Exec(shell command) to another command