Golang image.NRGBA and NRGBA64.Set and SetNRGBA(64) functions example
package image
Golang image.NRGBA and NRGBA64.Set and SetNRGBA(64) functions usage example
package main
import (
"fmt"
"image"
"image/color" // for color.NRGBA or color.NRGBA64
"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()
canvas := image.NewNRGBA(bounds) // or NewNRGBA64
canvas.Set(100, 100, image.Transparent)
// http://golang.org/pkg/image/color/#NRGBA
// NRGBA represents a non-alpha-premultiplied 32-bit color.
x := 512
y := 512
r := uint8(255) // type NRGBA or uint16 for type NRGBA64
g := uint8(255) // type NRGBA or uint16 for type NRGBA64
b := uint8(255) // type NRGBA or uint16 for type NRGBA64
a := uint8(255) // type NRGBA or uint16 for type NRGBA64
canvas.SetNRGBA(x, y, color.NRGBA{r,g,b,a})
}
References :
http://golang.org/pkg/image/#NRGBA.Set
http://golang.org/pkg/image/#NRGBA.SetNRGBA
Advertisement
Something interesting
Tutorials
+5.5k Golang : Intercept, inject and replay HTTP traffics from web server
+20k Golang : Append content to a file
+46.6k Golang : Encode image to base64 example
+14.9k Golang : Normalize unicode strings for comparison purpose
+8.3k Golang : Routes multiplexer routing example with regular expression control
+5.4k Swift : Convert string array to array example
+5.5k Golang : Return multiple values from function
+16.1k Golang : Read large file with bufio.Scanner cause token too long error
+12.6k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+19.5k Golang : Get RGBA values of each image pixel
+9.3k Golang : Generate Codabar
+9.8k Golang : Find correlation coefficient example