猿问

使用 Go 读取 Exif 元数据

有没有人有使用“github.com/gohugoio/hugo/resources/images/exif”从本地图像中提取元数据的示例?


我浏览了文档,因为我是 Go 新手,所以我不能 100% 确定我是否正在写东西。我确实阅读了图像,但我不确定下一步会是什么。


fname := "image.jpg"

f, err := os.Open(fname)

if err != nil {

  log.Fatal("Error: ", err)

}

(编辑1)其实我想我找到了一个解决方案:


d, err := exif.NewDecoder(exif.IncludeFields("File Type"))

x, err := d.Decode(f)

if err != nil {

  log.Fatal("Error: ", err)

}

fmt.Println(x)

但是,问题是我如何知道哪些字段可用?File Type例如返回<nil>


青春有我
浏览 187回答 1
1回答

largeQ

看起来 Hugo 使用github.com/rwcarlsen/goexif。go.dev 上的包文档显示Exif.Walk可以遍历每个非零 EXIF 字段的名称和标签。比如一个小程序:package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "log"&nbsp; &nbsp; "os"&nbsp; &nbsp; "github.com/rwcarlsen/goexif/exif"&nbsp; &nbsp; "github.com/rwcarlsen/goexif/tiff")type Printer struct{}func (p Printer) Walk(name exif.FieldName, tag *tiff.Tag) error {&nbsp; &nbsp; fmt.Printf("%40s: %s\n", name, tag)&nbsp; &nbsp; return nil}func main() {&nbsp; &nbsp; if len(os.Args) < 2 {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal("please give filename as argument")&nbsp; &nbsp; }&nbsp; &nbsp; fname := os.Args[1]&nbsp; &nbsp; f, err := os.Open(fname)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; x, err := exif.Decode(f)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; var p Printer&nbsp; &nbsp; x.Walk(p)}例子:$ go run main.go IMG_123.JPG&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ResolutionUnit: 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; YCbCrPositioning: 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Make: "Canon"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Model: "Canon IXUS 255 HS"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ThumbJPEGInterchangeFormat: 5620&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PixelYDimension: 3000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FocalPlaneResolutionUnit: 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GPSVersionID: [2,3,0,0]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ExifVersion: "0230"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WhiteBalance: 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DateTime: "2016:10:04 17:27:56"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CompressedBitsPerPixel: "5/1"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;... etc ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Orientation: 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MeteringMode: 5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FocalLength: "4300/1000"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PixelXDimension: 4000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InteroperabilityIFDPointer: 4982&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FocalPlaneXResolution: "4000000/244"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;XResolution: "180/1"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ComponentsConfiguration: ""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ShutterSpeedValue: "96/32"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ApertureValue: "101/32"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ExposureBiasValue: "-1/3"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FocalPlaneYResolution: "3000000/183"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SceneCaptureType: 0
随时随地看视频慕课网APP

相关分类

Go
我要回答