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
+11.1k Golang : 2 dimensional array example
+8k Golang : Intercept and compare HTTP response code example
+10.7k Golang : Split strings into command line arguments
+26.1k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+7.9k Golang : automatically figure out array length(size) with three dots
+73.1k Golang : How to return HTTP status code?
+7.6k Golang : Convert(cast) []byte to io.Reader type
+5k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+7.3k Golang : Metaprogramming example of wrapping a function
+12.5k Golang : How to get year, month and day?
+81.8k Golang : How to convert character to ASCII and back
+6.3k Golang : Calculate how many weeks left to go in a given year