eshalev
eshalev

Reputation: 3265

Convert image from *image.YCbCr to *image.RGBA

I am loading a jpeg from the disk, and I would like to do some RGBA operations on it.

However, since the jpeg is not RGBA, i get an error when I try to use it as such:

thumbnail,err:=jpeg.Decode(imageAsReader)
...
return thumbnail.(*image.RGBA)

yeileds the error:

interface conversion: image.Image is *image.YCbCr, not *image.RGBA

Is there an easy way to convert the image to RGBA once I have loaded it in memory? (Other operations are in RGBA later on, so that is the color model I want to use in memory).

Upvotes: 2

Views: 4188

Answers (1)

Graham King
Graham King

Reputation: 5720

As the comments suggest, you have to create a new image and draw into it: b := thumbnail.Bounds() m := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy())) draw.Draw(m, m.Bounds(), thumbnail, b.Min, draw.Src)

YCbCr is a sub-sampled image, so it doesn't map directly to the 4-bytes-per-pixel of RGBA.

Upvotes: 4

Related Questions