Is this a bug of "image" package?

Dear all:

when I use the “image” package to decode my image ,returns an error : image:unknown format
here is my code:
package main

import (
	"image"
	"os"
	"fmt"
)

func getImageInfo(imagePath string)  {

	src0, err := os.Open(imagePath)
	if err != nil {
		fmt.Println(err)
	}
	defer src0.Close()

	conf, _, err := image.DecodeConfig(src0)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(conf.Width)
		fmt.Println(conf.Height)
	}
}

func main() {
	getImageInfo("path_of _my_image")
}

but if I import “image/jpeg” and “image/png” at the same time ,it works well
here is the code:
package main

    import (
    	"image"
    	"os"
    	"fmt"
    	"image/png"
    	"image/jpeg"
    )

    func getImageInfo(imagePath string)  {
    	// just for import "image/png" and "image/jpeg"
    	fmt.Println(png.UnsupportedError("aa"))
    	fmt.Println(jpeg.UnsupportedError("aa"))
    	
    	src0, err := os.Open(imagePath)
    	if err != nil {
    		fmt.Println(err)
    	}
    	defer src0.Close()

    	conf, _, err := image.DecodeConfig(src0)
    	if err != nil {
    		fmt.Println(err)
    	} else {
    		fmt.Println(conf.Width)
    		fmt.Println(conf.Height)
    	}
    }

    func main() {
    	getImageInfo("/Users/guoxu/code/go/http/file_server/file/3.jpg")
    }

the output is:
png: unsupported feature: aa
unsupported JPEG feature: aa
200
200

so, I don’t know whether I used it by a wrong way or it is a bug ?

many thanks!

I recently used the image and image/jpeg libs in a blog article: https://appliedgo.net/imageprocessing/

See func openImage() - maybe this is what you are looking for. At least it worked for me! :slight_smile:

1 Like

Hey @guoxu3,

As it states in the image package’s documentation (image package - image - Go Packages), you have to register the formats that you want to use first:

Decoding any particular image format requires the prior registration of a decoder function. Registration is typically automatic as a side effect of initializing that format’s package so that, to decode a PNG image, it suffices to have

import _ “image/png”

in a program’s main package. The _ means to import a package purely for its initialization side effects.

Here are a bunch of image package examples that you can check out if you want to have a look: go-packages/image at master · radovskyb/go-packages · GitHub.

If you are unsure what decode function to use when you want to decode an image, just use something like filepath.Ext to first check the extension type and if you’re going to be working with multiple image formats, you can simply register all of the formats with the underscore importing.

You can check out the covert unknown format examples I have here as well if you want: go-packages/image/imageformats at master · radovskyb/go-packages · GitHub

1 Like

oh! I didn’t read the documentation carefully,that‘s my fault.
Thank you very much!

I used the lib by the wrong way. Thank you for your suggestion

No problemo :slight_smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.