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.8k Golang : Find and draw contours with OpenCV example
+6.4k Golang : Spell checking with ispell example
+12k Golang : md5 hash of a string
+35.1k Golang : Strip slashes from string example
+6.1k Golang & Javascript : How to save cropped image to file on server
+8.1k Swift : Convert (cast) Character to Integer?
+12.9k Golang : Calculate elapsed years or months since a date
+16.2k Golang : Check if a string contains multiple sub-strings in []string?
+4.5k Adding Skype actions such as call and chat into web page examples
+8.8k Golang : What is the default port number for connecting to MySQL/MariaDB database ?
+18.5k Golang : Implement getters and setters
+5.2k Golang : Generate Interleaved 2 inch by 5 inch barcode