douqin231881 2014-02-12 22:39
浏览 3384
已采纳

使用golang获取图像大小

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.

  • 写回答

1条回答 默认 最新

  • doudai3012 2014-02-12 23:11
    关注

    There are two problems with your code.

    The first one is that you have inverted the test err != nil, so you try to decode the image only in the case where you have an error. It should be err == nil.

    The second one, as said by jimt, is that you use filepath.Name(), which contains only the file name in os.Open(), this is what makes err to always be set, thus always entering in the if, and decoding a file that doesn't exists.

    Here is the corrected code :

    package main
    
    import (
        "fmt"
        "image"
        _ "image/jpeg"
        "io/ioutil"
        "os"
        "path/filepath"
    )
    
    const dir_to_scan string = "/home/da/to_merge"
    
    func main() {
        files, _ := ioutil.ReadDir(dir_to_scan)
        for _, imgFile := range files {
    
            if reader, err := os.Open(filepath.Join(dir_to_scan, imgFile.Name())); err == nil {
                defer reader.Close()
                im, _, err := image.DecodeConfig(reader)
                if err != nil {
                    fmt.Fprintf(os.Stderr, "%s: %v
    ", imgFile.Name(), err)
                    continue
                }
                fmt.Printf("%s %d %d
    ", imgFile.Name(), im.Width, im.Height)
            } else {
                fmt.Println("Impossible to open the file:", err)
            }
        }
    }
    

    Also, don't forget to add other imports than image/jpeg if you have other images formats in your dir.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波