Golang image.Alpha.SubImage function examples
package image
func (p *Alpha) 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.Alpha.SubImage function usage examples
Example 1:
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()
alpha := image.NewAlpha(bounds)
rect := image.Rect(0, 0, 100, 100)
subimg := alpha.SubImage(rect)
fmt.Println(subimg.Bounds())
fmt.Println(subimg.At(10, 10))
}
Example 2:
func getSubImage(src image.Image, rect image.Rectangle) image.Image {
if img, ok := src.(*image.NRGBA); ok {
return img.SubImage(rect)
}
if img, ok := src.(*image.NRGBA64); ok {
return img.SubImage(rect)
}
if img, ok := src.(*image.RGBA); ok {
return img.SubImage(rect)
}
if img, ok := src.(*image.RGBA64); ok {
return img.SubImage(rect)
}
if img, ok := src.(*image.Alpha); ok {
return img.SubImage(rect)
}
if img, ok := src.(*image.Alpha16); ok {
return img.SubImage(rect)
}
if img, ok := src.(*image.Gray); ok {
return img.SubImage(rect)
}
if img, ok := src.(*image.Gray16); ok {
return img.SubImage(rect)
}
if img, ok := src.(*image.Paletted); ok {
return img.SubImage(rect)
}
if img, ok := src.(*image.YCbCr); ok {
return img.SubImage(rect)
}
logger.Error("failed to get sub image")
return src
}
Reference :
Advertisement
Something interesting
Tutorials
+35.5k Golang : Smarter Error Handling with strings.Contains()
+7.9k Golang : How to feed or take banana with Gorilla Web Toolkit Session package
+9.7k Golang : Load ASN1 encoded DSA public key PEM file example
+11k How to test Facebook App on localhost ?
+16.3k Golang : convert string or integer to big.Int type
+8.2k Golang : Routes multiplexer routing example with regular expression control
+6.3k Golang : Selection sort example
+7.8k Golang : Reverse a string with unicode
+8.9k Golang : Gaussian blur on image and camera video feed examples
+3.7k Java : Random alphabets, alpha-numeric or numbers only string generator
+8.8k Golang : Executing and evaluating nested loop in html template
+5.6k Golang : Shortening import identifier