Golang image.NRGBA(64).SubImage function example
package image
func (p *NRGBA) SubImage(r Rectangle) Image
SubImage returns an image representing the portion of the image p visible through r. The returned value shares pixels with the original image.
Golang image.NRGBA(64).SubImage function usage example
package main
import (
"fmt"
"image"
"image/png"
"os"
)
func init() {
// without this register .. At(), Bounds() functions will
// caused memory pointer error!!
image.RegisterFormat("png", "png", png.Decode, png.DecodeConfig)
}
func main() {
imgfile, err := os.Open("./img.png")
if err != nil {
fmt.Println("img.png file not found!")
os.Exit(1)
}
defer imgfile.Close()
img, _, err := image.Decode(imgfile)
bounds := img.Bounds()
nrgba := image.NewNRGBA(bounds)
rect := image.Rect(0, 0, 100, 100)
subimg := nrgba.SubImage(rect)
fmt.Println(subimg.Bounds())
fmt.Println(subimg.At(10, 10))
}
Reference :
Advertisement
Something interesting
Tutorials
+14.3k Golang : Recombine chunked files example
+8.8k Golang : Random integer with rand.Seed() within a given range
+5.4k Javascript : How to loop over and parse JSON data?
+5k Golang : micron to centimeter example
+24.6k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+9.6k Golang : Read file with ioutil
+16.3k Golang : How to extract links from web page ?
+16.9k Golang : How to generate QR codes?
+20.4k nginx: [emerg] unknown directive "passenger_enabled"
+17k Golang : How to save log messages to file?
+12.1k Golang : Sort and reverse sort a slice of runes
+20.2k Golang : Reset or rewind io.Reader or io.Writer