Golang image.NewAlpha function examples
package image
func NewAlpha(r Rectangle) *Alpha
NewAlpha returns a new Alpha with the given bounds.
Golang image.NewAlpha function usage examples
Example 1:
package main
import (
"fmt"
"image"
"image/jpeg"
"os"
)
func init() {
// damn important or else At(), Bounds() functions will
// caused memory pointer error!!
image.RegisterFormat("jpeg", "jpeg", jpeg.Decode, jpeg.DecodeConfig)
}
func main() {
imgfile, err := os.Open("./img.jpg")
if err != nil {
fmt.Println("img.jpg file not found!")
os.Exit(1)
}
defer imgfile.Close()
img, _, err := image.Decode(imgfile)
fmt.Println(img.At(10, 10))
bounds := img.Bounds()
fmt.Println(bounds)
canvas := image.NewAlpha(bounds) // <-- here
// is this image opaque
op := canvas.Opaque()
fmt.Println(op)
}
Sample output :
{255 128 128}
(0,0)-(760,479)
false
Example 2:
inFile, openErr := os.Open(GIFFileInPath)
if openErr != nil {
fmt.Printf("couldn't open %v: %v\n", inPath, openErr)
os.Exit(1)
}
decoded, _, decodeErr := image.Decode(inFile)
inFile.Close()
// optimize image, converting colorspace if requested
bounds := decoded.Bounds()
switch optimizee := decoded.(type) {
case *image.Alpha16:
converted := image.NewAlpha(bounds)
...
Reference :
Advertisement
Something interesting
Tutorials
+6.1k PageSpeed : Clear or flush cache on web server
+4.8k PHP : Extract part of a string starting from the middle
+5.4k Javascript : How to loop over and parse JSON data?
+22.8k Golang : untar or extract tar ball archive example
+8.2k Golang : Get final or effective URL with Request.URL example
+10.9k Golang : Sieve of Eratosthenes algorithm
+9.1k Golang : Serving HTTP and Websocket from different ports in a program example
+8.9k Golang : GMail API create and send draft with simple upload attachment example
+17k Golang : Covert map/slice/array to JSON or XML format
+24.5k Golang : GORM read from database example
+22.1k Golang : Repeat a character by multiple of x factor
+12.8k Golang : Listen and Serve on sub domain example