Golang image.Decode function example
package image
func Decode(r io.Reader) (Image, string, error)
Decode decodes an image that has been encoded in a registered format. The string returned is the format name used during format registration. Format registration is typically done by an init function in the codec- specific package.
Golang image.Decode function usage example
package main
import (
"fmt"
"image"
"image/png"
"io/ioutil"
"os"
"bytes"
)
func init() {
image.RegisterFormat("png", "png", png.Decode, png.DecodeConfig)
}
func main() {
imgBuffer, err := ioutil.ReadFile("./img.png")
if err != nil {
fmt.Println("img.png file not found!")
os.Exit(1)
}
reader := bytes.NewReader(imgBuffer)
img, formatname, err := image.Decode(reader) // <--- here
if err != nil {
fmt.Println(err)
os.Exit(1)
}
//test
fmt.Println("Format : ", formatname)
fmt.Println("Bounds : ", img.Bounds())
fmt.Println("At : ", img.At(10,100))
fmt.Println("ColorModel : ", img.ColorModel())
}
Sample output (depending on image file ) :
Format : png
Bounds : (0,0)-(600,849)
At : {255 255 255 255}
ColorModel : &{0x82270}
Reference :
Advertisement
Something interesting
Tutorials
+14.5k Golang : How to check if your program is running in a terminal
+52.6k Golang : How to get struct field and value by name
+20.2k Golang : Count number of digits from given integer value
+10.1k Golang : Test a slice of integers for odd and even numbers
+9.9k Golang : ffmpeg with os/exec.Command() returns non-zero status
+6.9k Golang : Fibonacci number generator examples
+18.5k Golang : Set, Get and List environment variables
+6.1k Golang : Scan forex opportunities by Bollinger bands
+19.3k Golang : Get RGBA values of each image pixel
+10.8k Android Studio : Checkbox for user to select options example
+9.5k Golang : Changing a RGBA image number of channels with OpenCV
+22.2k Golang : Securing password with salt