解码图像不支持的类型错误

huangapple go评论79阅读模式
英文:

go decode image unsupported type error

问题

我正在尝试使用内置的"image"包打开一个png图像,代码如下:

infile, err := os.Open(filename)
image.RegisterFormat("png", "png", png.Decode, png.DecodeConfig)
src, _, err := image.Decode(infile)

image.Decode函数产生了一个"unsupported type *image.RGBA"的错误。有人对这个错误有什么见解吗?

我还尝试了使用相应的注册来打开JPEG图像:

image.RegisterFormat("png", "png", png.Decode, png.DecodeConfig)
src, _, err := image.Decode(infile)

结果是"unsupported type *image.YCbCr"。这非常令人困惑,因为图像本身是RGB格式的。

编辑:我还尝试了只导入image/jpegimage/png,而不使用image.RegisterFormat,但仍然得到相同的错误。

编辑2:抱歉,我得到的错误甚至不是来自Decode函数。图像可以正确解码。

英文:

I'm trying to open a png image using the built in "image" package as such:

infile, err := os.Open(filename)
image.RegisterFormat("png", "png", png.Decode, png.DecodeConfig)
src, _, err := image.Decode(infile)

The image.Decode function is producing an error of unsupported type *image.RGBA. Anyone have any insight into this error?

I also tried this with a JPEG with the corresponding registration:

image.RegisterFormat("png", "png", png.Decode, png.DecodeConfig)
src, _, err := image.Decode(infile)

Which results in unsupported type *image.YCbCr. Very confusing as the image itself is in RGB.

Edit: also tried just importing image/jpeg and image/png, without using image.RegisterFormat, but still getting the same errors.

Edit #2: Apologies, the error I was getting was not even coming from the Decode function. The images decode properly.

答案1

得分: 4

首先是错误:

在注册格式时,你犯了一个错误。

PNG 的魔术值不是 "png",而是 "\x89PNG\r\n\x1a\n"。所以注册应该是:

image.RegisterFormat("png", "\x89PNG\r\n\x1a\n", png.Decode, png.DecodeConfig)

JPEG 的魔术值不是 "jpeg",而是 "\xff\xd8"。JPEG 的注册应该是:

image.RegisterFormat("jpeg", "\xff\xd8", jpeg.Decode, jpeg.DecodeConfig)

但是不要这样做!

只需导入 image/pngimage/jpeg 包,包的初始化函数会自动为你完成这些操作。如果你不使用这些包(只在初始化“副作用”时这样做),你可以使用空白标识符:

import (
    _ "image/png"
    _ "image/jpeg"
)

在上述导入之后,你将能够解码 PNG 和 JPEG 图像。

英文:

First the mistakes:

You make a mistake when registering the formats.

The PNG magic is not "png" but "\x89PNG\r\n\x1a\n". So registration is:

image.RegisterFormat("png", "\x89PNG\r\n\x1a\n", png.Decode, png.DecodeConfig)

The JPEG magic is not "jpeg" but "\xff\xd8". JPEG registration:

image.RegisterFormat("jpeg", "\xff\xd8", jpeg.Decode, jpeg.DecodeConfig)

BUT don't do this!

Simply import the image/png and image/jpeg packages, the package init functions do this automatically for you. You may use the blank identifier if you don't use the packages (and you only do this for the initialization "side-effect"):

import (
    _ "image/png"
    _ "image/jpeg"
)

After the above imports, you will be able to decode PNG and JPEG images.

答案2

得分: 0

很好的回答,@icza。简单点,只需导入“image/jpeg”和“image/png”包(如果我们需要处理两种图像格式的话。否则,只导入你要使用的特定包,要么是“image/jpeg”,要么是“image/png”)。这对我有效。

import (
    ...
    .
    _ "image/jpeg"
    _ "image/png"
    .
    . 
    ....
)
英文:

Great answer by @icza. Make it simple just import packages "image/jpeg" and "image/png" (If we have to work with both image format.Otherwise import only specific package you gonna use either "image/jpeg" or "image/png"). It works for me.

import (
    ...
	.
    . 
	_ "image/jpeg"
	_ "image/png"
	.
    . 
    ....
)

huangapple
  • 本文由 发表于 2017年7月14日 21:50:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/45104707.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定