Home >Backend Development >Golang >Is there a library for Go that can open paletted pngs with 2-bit color depth?

Is there a library for Go that can open paletted pngs with 2-bit color depth?

PHPz
PHPzforward
2024-02-09 23:30:21370browse

Go 是否有可以打开 2 位颜色深度的调色板 png 的库?

php Editor Xigua Go is a powerful programming language, but is there a library that can open palette PNG with 2-bit color depth? The answer is yes. The Go language has many libraries and tools for working with images, some of which can open and process paletted PNG images at specific depths. By using these libraries, you can easily read and edit paletted PNG images with 2-bit color depth, adding more functionality and flexibility to your applications. Whether you are a beginner or an experienced Go developer, these libraries can help you achieve your goals, providing better image processing and editing capabilities.

Question content

How to use go to read palette-based png images?

For images in python I can simply do the following:

from pil import image

im = image.open('image.png')
pix = im.load()
for i in range(100):
    for j in range(100):
        print(pix[i, j])

Use go:

f, err := os.open("image.png")
    if err != nil {
        log.fatal(err)
    }
    defer f.close()

    pal, ok := cfg.colormodel.(color.palette) // ok is true
    if ok {
        for i := range pal {
            cr, cg, cb, ca := pal[i].rgba()
            fmt.printf("pal[%3d] = %5d,%5d,%5d,%5d\n", i, cr, cg, cb, ca)
        }
    }

    img, err := png.decode(f)
    if err != nil {
        log.fatal(err) // fails here!!
    }

    for y := img.bounds().min.y; y < img.bounds().max.y; y++ {
        for x := img.bounds().min.x; x < img.bounds().max.x; x++ {
            log.println("img.at(x, y):", img.at(x, y))
        }
        fmt.print("\n")
    }

"png: invalid format: not a png file" will be thrown when decoding.

If I use the file command on mac shell, it says:

image.png: png image data, 100 x 100, 2-bit colormap, non-interlaced

vscode renders images very well.

I tried this on both an image created by Adobe Illustrator and an image generated by the following code. Both encounter the same error:

func createPNG() {
    // Create a new image with the desired dimensions
    img := image.NewPaletted(image.Rect(0, 0, 100, 100), color.Palette{
        color.RGBA{255, 0, 0, 255}, // Red
        color.RGBA{0, 255, 0, 255}, // Green
        color.RGBA{0, 0, 255, 255}, // Blue
    })

    // Set the pixel colors in the image
    for x := 0; x < 100; x++ {
        for y := 0; y < 100; y++ {
            switch {
            case x < 50 && y < 50:
                img.SetColorIndex(x, y, 0) // Set the pixel at (x, y) to red
            case x >= 50 && y < 50:
                img.SetColorIndex(x, y, 1) // Set the pixel at (x, y) to green
            case x < 50 && y >= 50:
                img.SetColorIndex(x, y, 2) // Set the pixel at (x, y) to blue
            default:
                img.SetColorIndex(x, y, 3) // Set the pixel at (x, y) to transparent
            }
        }
    }

    // Create a new file to save the PNG image
    file, err := os.Create("image.png")
    if err != nil {
        panic(err)
    }
    defer file.Close()

    // Encode the image as a PNG and save it to the file
    err = png.Encode(file, img)
    if err != nil {
        panic(err)
    }
}

Solution

It seems that in your case it is not the format of the image, but the way you are using the image file.

I assume you are passing it to image.DecodeConfig() first (the code doesn't show it, but cfg must have been initialized) and then to image.Decode ().

The problem is that after the first call, your file has an offset, but the second call assumes it is reading from the beginning of the file.

You can solve this problem by rolling back the file after reading the configuration:

File.Seek(0, io.SeekStart)

The above is the detailed content of Is there a library for Go that can open paletted pngs with 2-bit color depth?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete