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

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

图片转代码服务由CSDN问答提供 功能建议

我是golang的新手,我正在尝试获取目录中列出的所有图像的图像大小 。 这就是我要做的

 包main 
 
import(
“ fmt” 
“ image” 
 _“ image / jpeg” 
“ io /  ioutil“ 
” os“ 
)
 
const dir_to_scan string =” / home / da / to_merge“ 
 
func main(){
个文件,_:= ioutil.ReadDir(dir_to_scan)
为_  ,文件路径:=范围文件{
 
,如果读者使用,错误:= os.Open(filepath.Name());  err!= nil {
延迟reader.Close()
 im,_,err:= image.DecodeConfig(reader)
如果err!= nil {
 fmt.Fprintf(os.Stderr,“%s:  %v 
“,filepath.Name(),err)
继续
} 
 fmt.Printf(”%s%d%d 
“,filepath.Name(),im.Width,im.Height  )
} else {
 fmt.Println(“无法打开文件”)
} 
} 
} 
   
 
 

出现 image.DecodeConfig 时出错,即表示 image:未知格式 有人对正确的操作方法有想法吗? 在这里的文档中 http://golang.org/src/pkg/image/format。 go?s = 2676:2730#L82 说我应该传递 io.Reader 作为参数,这就是我正在做的。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题