将图像从* image.YCbCr转换为* image.RGBA [英] Convert image from *image.YCbCr to *image.RGBA

查看:73
本文介绍了将图像从* image.YCbCr转换为* image.RGBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从磁盘加载jpeg,我想在其中执行一些RGBA操作.

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

但是,由于jpeg不是RGBA,因此当我尝试使用jpeg时出现错误:

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)

产生错误:

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

将图像加载到内存后,是否有一种简单的方法可以将图像转换为RGBA?(其他操作稍后将在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).

推荐答案

正如评论所建议的,您必须创建一个新图像并将其绘制到其中:b:= thumbnail.Bounds()m:= image.NewRGBA(image.Rect(0,0,b.Dx(),b.Dy()))draw.Draw(m,m.Bounds(),缩略图,b.Min,draw.Src)

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是二次采样的图像,因此它不直接映射到RGBA的每像素4字节.

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

这篇关于将图像从* image.YCbCr转换为* image.RGBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆