DecodeConfig配置函数的返回类型是struct image,它具有2个int变量Width和Height,通过它们可以获取Go中任何jpg jpeg gif或png图像的尺寸。
package main import ( "fmt" "image" "os" _ "image/jpeg" _ "image/png" _ "image/gif" ) func main() { imagePath := "jellyfish.jpg" file, err := os.Open(imagePath) defer file.Close(); if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) } image, _, err := image.DecodeConfig(file) // Image 结构 if err != nil { fmt.Fprintf(os.Stderr, "%s: %v\n", imagePath, err) } fmt.Println("Width:", image.Width, "Height:", image.Height) }
C:\golang\codes>go run example.go
宽度:758高度:569
C:\golang\codes>
宽度:758高度:569
C:\golang\codes>