Golang image.PalettedImage type example
package image
PalettedImage is an image whose colors may come from a limited palette. If m is a PalettedImage and m.ColorModel() returns a PalettedColorModel p, then m.At(x, y) should be equivalent to p[m.ColorIndexAt(x, y)]. If m's color model is not a PalettedColorModel, then ColorIndexAt's behavior is undefined.
Golang image.PalettedImage type usage example
package main
import (
"fmt"
"image"
"image/png"
"image/color"
"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)
var pal color.Palette
if _, ok := img.(image.PalettedImage); ok {
pal, _ = img.ColorModel().(color.Palette)
// test
fmt.Println(pal)
}
// otherwise image is not PalettedImage type
fmt.Println("img.png is not PalettedImage type")
}
Sample output (depending on input image ) :
img.png is not PalettedImage type
Reference :
Advertisement
Something interesting
Tutorials
+26.6k Golang : Encrypt and decrypt data with AES crypto
+12.6k Golang : flag provided but not defined error
+7.8k Golang : Lock executable to a specific machine with unique hash of the machine
+5.3k Python : Convert(cast) string to bytes example
+6.2k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+6.6k Golang : How to validate ISBN?
+13k Golang : Get terminal width and height example
+10.5k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+17.7k Golang : Read data from config file and assign to variables
+15.6k Golang : How to convert(cast) IP address to string?
+5.3k Swift : Convert string array to array example
+36.3k Golang : Convert(cast) int64 to string