如何在Go中打开图像以获取黑白像素的二进制数据?

reticentroot:

我一直在尝试用Go以二进制模式打开图像。在Python中,我将使用Pillow和image.open()(rb模式)。例。

img = Image.open("PNG.png")
pix = img.getdata()  #where 0 is black and 1 is white pixel

这将使用非常干净的白点和黑点二进制打开图像,如下图所示。例在go中,我尝试os.Open(file.jpg)打开文件。.我尝试使用对其进行解码image.Decode(),将文件加载到中bytes.Buffer,尝试过fmt.Sprintf("%b", data),所有解决方案都提供一个字节数组。将该字节数组转换为二进制文件看起来与上面的图像完全不同。我也尝试过encoding/binary,只是获得字节而生成相同的故事,而生成的二进制文件不是我想要的...

最近我尝试过

package main

import (
    "fmt"
    "image"
    "image/jpeg"
    "io"
    "log"
    "os"
)

// Pixel struct example
type Pixel struct {
    R int
    G int
    B int
    A int
}

func main() {
    // You can register another format here
    image.RegisterFormat("jpg", "jpg", jpeg.Decode, jpeg.DecodeConfig)

    file, err := os.Open("/Users/marcsantiago/Desktop/2033bb1b194adace86f99c7bb7d72e81.jpg")

    if err != nil {
        log.Fatalln("Error: File could not be opened")

    }

    defer file.Close()

    pixels, err := getPixels(file)

    if err != nil {
        log.Fatalln("Error: Image could not be decoded")
    }
    black := Pixel{0, 0, 0, 255}

    for i := range pixels {
        if pixels[i] == black {
            fmt.Print("0")
        } else {
            fmt.Print("1")
        }

    }
}

func getPixels(file io.Reader) ([]Pixel, error) {
    img, _, err := image.Decode(file)

    if err != nil {
        return nil, err
    }

    bounds := img.Bounds()
    width, height := bounds.Max.X, bounds.Max.Y

    var pixels []Pixel
    for y := 0; y < height; y++ {
        for x := 0; x < width; x++ {
            pixels = append(pixels, rgbaToPixel(img.At(x, y).RGBA()))
        }
    }
    return pixels, nil
}

// img.At(x, y).RGBA() returns four uint32 values; we want a Pixel
func rgbaToPixel(r uint32, g uint32, b uint32, a uint32) Pixel {
    return Pixel{int(r / 257), int(g / 257), int(b / 257), int(a / 257)}
}

这样我就可以将二进制文件与我期望的二进制文件进行比较,我将rgba转换为1和0s,其中0 == black ...它仍然不匹配甚至不匹配。在此处输入图片说明

请帮助。我没主意了。PS。该网站http://www.dcode.fr/binary-image,还会打开图像并生成我期望的数据。

更新:这是我正在使用的图像。要解码的图像

peterSO:

例如,

package main

import (
    "bytes"
    "fmt"
    "image"
    "os"

    _ "image/jpeg"
)

func main() {
    fName := "ggk3Z.jpg"
    f, err := os.Open(fName)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
    defer f.Close()
    img, _, err := image.Decode(f)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }

    // http://www.dcode.fr/binary-image
    var txt bytes.Buffer
    bounds := img.Bounds()
    for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
        for x := bounds.Min.X; x < bounds.Max.X; x++ {
            r, g, b, _ := img.At(x, y).RGBA()
            bin := "0"
            if float64((r+g+b))/3 > 0.5 {
                bin = "1"
            }
            txt.WriteString(bin)
        }
        txt.WriteString("\n")
    }
    fmt.Fprint(os.Stdout, txt.String())
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从包含二进制像素数据的数组列表中创建新图像

如何在MATLAB中获取二进制图像中字符的外周长?

如何在Go中读取二进制文件

如何从PHP中的二进制数据在浏览器上获取图像

如何使用PHP从二进制Blob数据中获取BMP图像

如何在objcopy生成的二进制图像文件中获取入口地址?

如何在SQL中获取二进制图像

从Python中的二进制图像获取像素边界坐标(不是边缘)

在python opencv中获取强度为255的二进制图像的像素位置

如何在Go二进制文件中捆绑SQLite数据库?

如何在Go中读取打包的二进制数据?

如何在pyspark中读取二进制数据

黑白图像到二进制数组

如何在python中获取内存中复杂对象的二进制表示(打开“rb”)?

将黑白图像加载到二进制数组中

使用jQuery从二进制数据获取图像

如何在python中打开parquet(二进制数据类型)文件而不会出现RAM错误?

如何在PHRETS中将二进制图像数据显示为图像

在matplotlib中绘制黑白二进制图

如何获取CasperJS请求的图像的二进制文件?

如何在ASP.NET中使用C#从数据库中检索二进制图像

如果可能的话,您如何在 memcached 中存储二进制图像数据?

是否可以从React-Native中的图像获取二进制数据?

使用 Magick Wand API 从图像中获取二进制数据

如何从WebSocket二进制数据显示图像?

如何从包含二进制数据的str构建图像?

在CSV文件中传输二进制数据(图像等)

在Go中通过HTTP接收二进制数据

如何在React.js中将二进制数据转换为图像