将PNG图像转换为原始的[]byte字节切片,使用Golang。

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

Convert PNG image to raw []byte Golang

问题

我有一个以PNG格式存储的图像,它是一个1x512维的数组。我需要将其转换为没有PNG格式的原始字节。请问如何在Go语言中将PNG转换为原始字节?

我有一些Python代码可以实现我想要的功能,但是我找不到在Go语言中实现相同功能的方法:

  1. import (
  2. "bytes"
  3. "image"
  4. _ "image/png"
  5. "io/ioutil"
  6. )
  7. func PNGToRawBytes(pngData []byte) ([]byte, error) {
  8. img, _, err := image.Decode(bytes.NewReader(pngData))
  9. if err != nil {
  10. return nil, err
  11. }
  12. buf := new(bytes.Buffer)
  13. err = img.(image.Image).RGBA().Encode(buf)
  14. if err != nil {
  15. return nil, err
  16. }
  17. return ioutil.ReadAll(buf)
  18. }

你可以使用上面的Go代码将PNG转换为原始字节。调用PNGToRawBytes函数并传入PNG数据作为参数,它将返回转换后的原始字节。

英文:

I have an image in PNG format which is just an array of dimensions 1x512.
I needs its raw bytes without the PNG format. How can I convert the PNG to raw bytes in Go.

I have some python code which does what I want, but I have not been able to find the same funtionality in Go:

  1. image = Image.open(io.BytesIO(features))
  2. array = np.frombuffer(image.tobytes(), dtype=np.float32)

答案1

得分: 1

这是一个比你的解决方案更通用的解决方案。它使用图像本身的尺寸而不是硬编码的值。

  1. func imageToRGBA(img image.Image) []uint8 {
  2. sz := img.Bounds()
  3. raw := make([]uint8, (sz.Max.X-sz.Min.X)*(sz.Max.Y-sz.Min.Y)*4)
  4. idx := 0
  5. for y := sz.Min.Y; y < sz.Max.Y; y++ {
  6. for x := sz.Min.X; x < sz.Max.X; x++ {
  7. r, g, b, a := img.At(x, y).RGBA()
  8. raw[idx], raw[idx+1], raw[idx+2], raw[idx+3] = uint8(r), uint8(g), uint8(b), uint8(a)
  9. idx += 4
  10. }
  11. }
  12. return raw
  13. }
英文:

Here's a slightly more generic solution than yours. It uses the dimensions of the image itself instead of hardcoded values.

  1. func imageToRGBA(img image.Image) []uint8 {
  2. sz := img.Bounds()
  3. raw := make([]uint8, (sz.Max.X-sz.Min.X)*(sz.Max.Y-sz.Min.Y)*4)
  4. idx := 0
  5. for y := sz.Min.Y; y &lt; sz.Max.Y; y++ {
  6. for x := sz.Min.X; x &lt; sz.Max.X; x++ {
  7. r, g, b, a := img.At(x, y).RGBA()
  8. raw[idx], raw[idx+1], raw[idx+2], raw[idx+3] = uint8(r), uint8(g), uint8(b), uint8(a)
  9. idx += 4
  10. }
  11. }
  12. return raw
  13. }

答案2

得分: 0

我找到了一个解决方案:

请注意,这段代码将图像在x轴上的值复制,并且x的最大值为512!

  1. const FeatureVectorDimensionLength = 512
  2. func imageToRaw(img image.Image) [2048]byte {
  3. var b [FeatureVectorDimensionLength*4]byte
  4. for i := 0; i < FeatureVectorDimensionLength; i++ {
  5. nrgba := img.At(i, 0).(color.NRGBA)
  6. idx := i*4
  7. b[idx], b[idx+1], b[idx+2], b[idx+3] = nrgba.R, nrgba.G, nrgba.B, nrgba.A
  8. }
  9. return b
  10. }
英文:

I be found a solution:

Please note that this copies the values in the image on the x-axis and the x max is 512!

  1. const FeatureVectorDimensionLength = 512
  2. func imageToRaw(img image.Image) [2048]byte {
  3. var b [FeatureVectorDimensionLength*4]byte
  4. for i := 0; i &lt; FeatureVectorDimensionLength; i++ {
  5. nrgba := img.At(i, 0).(color.NRGBA)
  6. idx := i*4
  7. b[idx], b[idx+1], b[idx+2], b[idx+3] = nrgba.R, nrgba.G, nrgba.B, nrgba.A
  8. }
  9. return b
  10. }

huangapple
  • 本文由 发表于 2022年2月16日 20:22:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/71141811.html
匿名

发表评论

匿名网友

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

确定