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
+9.3k Golang : Generate 403 Forbidden to protect a page or prevent indexing by search engine
+25k Golang : Convert file content into array of bytes
+3.5k Javascript : How to show different content with noscript?
+3.9k Which content-type(MIME type) to use for JSON data
+7.8k Golang : Capture text return from exec function example
+20.6k Golang : How to reverse slice or array elements order
+21.1k Golang : How to read JPG(JPEG), GIF and PNG files ?
+13.2k Golang : On enumeration
+11.4k Golang : "https://" not allowed in import path
+9.9k Android Studio : Create custom icons for your application example
+5.6k Golang : Gargish-English language translator
+9.9k Golang : Intercept and process UNIX signals example