I'm new to golang and I'm trying to get the image size of all the images listed in a directory. That's what I've done
package main
import (
"fmt"
"image"
_ "image/jpeg"
"io/ioutil"
"os"
)
const dir_to_scan string = "/home/da/to_merge"
func main() {
files, _ := ioutil.ReadDir(dir_to_scan)
for _, filepath := range files {
if reader, err := os.Open(filepath.Name()); err != nil {
defer reader.Close()
im, _, err := image.DecodeConfig(reader)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v
", filepath.Name(), err)
continue
}
fmt.Printf("%s %d %d
", filepath.Name(), im.Width, im.Height)
} else {
fmt.Println("Impossible to open the file")
}
}
}
I've an error when it comes to image.DecodeConfig
, that says image: unknown format
Has someone an idea about the proper way to do it?
In the docs here http://golang.org/src/pkg/image/format.go?s=2676:2730#L82 says that i should pass a io.Reader
as argument, and that's what i'm doing.