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
+7.7k Golang : Gomobile init produce "iphoneos" cannot be located error
+9.7k Golang : Sort and reverse sort a slice of integers
+29.5k Golang : Get and Set User-Agent examples
+10k Golang : How to check if a website is served via HTTPS
+13.5k Golang : How to determine if a year is leap year?
+19.7k Golang : How to get own program name during runtime ?
+17.5k Golang : Login and logout a user after password verification and redirect example
+5.8k Golang : Grab news article text and use NLP to get each paragraph's sentences
+14.5k Golang : Normalize unicode strings for comparison purpose
+6.6k Golang : Calculate pivot points for a cross
+11.1k Golang : How to flush a channel before the end of program?
+11.9k Golang : convert(cast) string to integer value