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
+5.7k How to let Facebook Login button redirect to a particular URL ?
+4.2k Golang : Pad file extension automagically
+3.6k Chrome : How to block socketloop.com links in Google SERP?
+5.8k Golang : Calculate BMI and risk category
+9.9k Golang : Fix fmt.Scanf() on Windows will scan input twice problem
+12.7k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+8.6k Golang : Sort and reverse sort a slice of integers
+15.3k Golang : How to implement two-factor authentication?
+5.9k Golang : How to detect if a sentence ends with a punctuation?
+7.8k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+18k Mac OSX : Homebrew and Golang
+8.6k Golang : List available AWS regions