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
+19.5k Golang : Calculate entire request body length during run time
+24.6k Golang : Change file read or write permission example
+14.7k How to automatically restart your crashed Golang server
+23.1k Golang : Calculate time different
+30.2k Golang : Get time.Duration in year, month, week or day
+10.8k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+17.1k Golang : Set up source IP address before making HTTP request
+21.1k Golang : Underscore or snake_case to camel case example
+38.4k Golang : Converting a negative number to positive number
+6.4k Apt-get to install and uninstall Golang
+6k Golang : Launching your executable inside a console under Linux