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
+19k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+20.6k Golang : Secure(TLS) connection between server and client
+14.1k Golang : Chunk split or divide a string into smaller chunk example
+13.8k Golang : Get dimension(width and height) of image file
+17.8k Golang : How to make a file read only and set it to writable again?
+5.7k Fix yum-complete-transaction error
+29.4k Golang : Login(Authenticate) with Facebook example
+13.6k Golang : Tutorial on loading GOB and PEM files
+6.4k Grep : How to grep for strings inside binary data
+10.5k Golang : Select region of interest with mouse click and crop from image
+14.3k Golang : How to convert a number to words
+11k Golang : Web routing/multiplex example