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

使用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.

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

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?