Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert image.Image to image.NRGBA

Tags:

image

go

When I call png.Decode(imageFile) it returns a type image.Image. But I can't find a documented way to convert this to an image.NRGBA or image.RGBA on which I can call methods like At().

How can I achieve this?

like image 377
Andy Avatar asked Jul 16 '15 20:07

Andy


1 Answers

If you don't need to "convert" the image type, and just want to extract the underlying type from the interface, use a "type assertion":

if img, ok := i.(*image.RGBA); ok {
    // img is now an *image.RGBA
}

Or with a type switch:

switch i := i.(type) {
case *image.RGBA:
    // i in an *image.RGBA
case *image.NRGBA:
    // i in an *image.NRBGA
}
like image 104
JimB Avatar answered Oct 28 '22 07:10

JimB