Golang image.Gray.SubImage function example
package image
func (p *Gray) 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.Gray.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()
gray := image.NewGray(bounds)
rect := image.Rect(0, 0, 100, 100)
subimg := gray.SubImage(rect)
fmt.Println(subimg.Bounds())
fmt.Println(subimg.At(10, 10))
}
Reference :
Advertisement
Something interesting
Tutorials
+11.9k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+13.7k Golang : Check if an integer is negative or positive
+11.7k How to tell if a binary(executable) file or web application is built with Golang?
+4.7k MariaDB/MySQL : Form select statement or search query with Chinese characters
+5.1k Swift : Convert (cast) Float to Int or Int32 value
+18.8k Golang : How to make function callback or pass value from function as parameter?
+11.6k SSL : The certificate is not trusted because no issuer chain was provided
+18.6k Golang : Generate thumbnails from images
+13.9k Golang : Get current time
+6.1k Golang : Measure execution time for a function
+11.5k Golang : Handle API query by curl with Gorilla Queries example