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
+4.3k Python : Find out the variable type and determine the type with simple test
+8.1k Python : Fix SyntaxError: Non-ASCII character in file, but no encoding declared
+5.4k Golang : Struct field tags and what is their purpose?
+43.8k Golang : Use wildcard patterns with filepath.Glob() example
+7.3k Golang : How to execute code at certain day, hour and minute?
+31.4k Golang : Validate email address with regular expression
+3.4k Java : Random alphabets, alpha-numeric or numbers only string generator
+9.5k Golang : Bcrypting password
+10.4k PHP : Convert(cast) bigInt to string
+14k Golang : Overwrite previous output with count down timer
+5.2k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday