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.8k Fix fatal error: evacuation not done in time problem
+7.8k Golang : GMail API create and send draft with simple upload attachment example
+4.7k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday
+14.2k Golang : How to add color to string?
+6.1k Golang : Validate credit card example
+6.2k Golang : Trim everything onward after a word
+3.9k Golang : Calculate a pip value and distance to target profit example
+6.3k Golang : Getting Echo framework StartAutoTLS to work
+6.6k Golang : Convert(cast) io.Reader type to string
+18k Golang : Execute shell command
+47k Golang : Upload file from web browser to server
+33.7k Golang : Strip slashes from string example