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
+10.7k Golang : How to detect a server/machine network interface capabilities?
+10k Golang : How to use if, eq and print properly in html template
+4.7k Linux/Unix/PHP : Restart PHP-FPM
+11.5k Golang : Get terminal width and height example
+21.3k Golang : Round float to precision example
+28.6k Golang : How to get HTTP request header information?
+14.1k Golang : How do I get the local IP (non-loopback) address ?
+4.3k Javascript : Change page title to get viewer attention
+6.8k Golang : Error reading timestamp with GORM or SQL driver
+18.8k Golang : Read directory content with os.Open
+7.6k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?