0

我需要从图像的 EXIF 元数据中获取一个包含信息的字符串。例如,我想:

相机型号,35mm 格式焦距:24mm, F11, 1/500, ISO 200

我可以看到所有的信息

identify -format '%[EXIF:*]' image.jpg

但是,我在合并输出和生成信息时遇到了麻烦。

第一个问题,当'%[EXIF:*]'打印所有 EXIF 数据时,如果我用特定的 EXIF 标签替换星号,它不会打印出任何东西。我知道我可以简单地打印出所有 EXIF 数据几次并使用它grep来获取我需要的数据,然后将它们组合在一起,但只检索我正在寻找的值感觉更好。

第二个问题,光圈值FNumber是“63/10”这样的格式,但我需要它是6.3;令人讨厌的是,快门速度ExposureTime是 10/5000,我需要它是 1/500。每种情况我需要什么样的转换?

谢谢!

4

4 回答 4

1

这里有一些东西可以让您开始使用awk它来查找 EXIF 关键字并在它们过去时保存相应的设置。然后在END它打印它找到的所有内容:

#!/bin/bash

identify -format '%[EXIF:*]' a.jpg | awk -F= '
   /^exif:Model/                 {model=$2}
   /^exif:FocalLengthIn35mmFilm/ {focal=$2}
   /^exif:FNumber/               {f=$2}
   /^exif:ExposureTime/          {t=$2}
   /^exif:ISOSpeedRatings/       {ISO=$2}
   END {
      # Check if fNumber is a rational number and refactor if it is
      n=split(f,k,"/")
      if(n==2){
         f=sprintf("%.1f",k[1]/k[2]);
      }
      # Check if exposure time is a rational number and refactor if it is
      n=split(t,k,"/")
      if(n==2){
         m=int(k[2]/k[1]);
         t=sprintf("1/%d",m);
      }
      print model,focal,f,t,ISO
   }' OFS=,

样本输出

iPhone 4,35,2.8,1/914,80

我没有太广泛地测试有理数的转换......在节日威士忌之间......

于 2017-12-30T17:08:59.977 回答
1

更简单的是直接使用EXIFTOOL。

infile="P1050001.JPG"
exiftool -model -FocalLengthIn35mmFormat -FNumber -ExposureTime -ISO "$infile"

Camera Model Name               : DMC-FZ30
Focal Length In 35mm Format     : 35 mm
F Number                        : 2.8
Exposure Time                   : 1/8
ISO                             : 200

或者

infile="P1050001.JPG"
exiftool -csv -model -FocalLengthIn35mmFormat -FNumber -ExposureTime -ISO "$infile"

P1050001.JPG,DMC-FZ30,35 mm,2.8,1/8,200
于 2017-12-30T19:35:34.590 回答
1

jzxu wrote:这也很好,但是,我注意到参数 -csv,我如何摆脱逗号并用空格替换它们?

unix 上的一种方法是简单地使用 tr 将命令替换为空格,如下所示:

exiftool -csv -model -FocalLengthIn35mmFormat -FNumber -ExposureTime -ISO "$infile" | tr "," " "

SourceFile Model FocalLengthIn35mmFormat FNumber ExposureTime ISO
P1050001.JPG DMC-FZ30 35 mm 2.8 1/8 200


或者,如果您不想要标题行:

exiftool -csv -model -FocalLengthIn35mmFormat -FNumber -ExposureTime -ISO "$infile" | tail -n +2 | tr "," " "

P1050001.JPG DMC-FZ30 35 mm 2.8 1/8 200


可能还有其他内部 EXIFTOOL 格式选项。见https://sno.phy.queensu.ca/~phil/exiftool/exiftool_pod.html

我不是 EXIFTOOL 的专家,所以也许我错过了一些东西。但我没有看到空格分隔的输出格式。但是,这会以制表符分隔格式输出。

infile="P1050001.JPG"
exiftool -s3 -T -model -FocalLengthIn35mmFormat -FNumber -ExposureTime -ISO "$infile"

DMC-FZ30    35 mm   2.8 1/8 200


因此,可以使用 tr 将制表符替换为空格。

exiftool -s3 -T -model -FocalLengthIn35mmFormat -FNumber -ExposureTime -ISO "$infile" | tr "\t" " "

DMC-FZ30 35 mm 2.8 1/8 200
于 2017-12-30T23:40:39.657 回答
0

这在 Imagemagick 6.9.9.29 Q16 Mac OSX 中对我来说很好。

infile="P1050001.JPG"
cameramodel=`identify -ping -format "%[EXIF:model]" "$infile"`
focallenght35=`identify -ping -format "%[EXIF:FocalLengthIn35mmFilm]" "$infile"`
fnumber1=`identify -ping -format "%[EXIF:FNumber]" "$infile" | cut -d/ -f1`
fnumber2=`identify -ping -format "%[EXIF:FNumber]" "$infile" | cut -d/ -f2`
exptime1=`identify -ping -format "%[EXIF:ExposureTime]" "$infile" | cut -d/ -f1`
exptime2=`identify -ping -format "%[EXIF:ExposureTime]" "$infile" | cut -d/ -f2`
isospeed=`identify -ping -format "%[EXIF:ISOSpeedRatings]" "$infile"`
fnumber=`echo "scale=1; $fnumber1/$fnumber2" | bc`
exptime=`echo "scale=3; $exptime1/$exptime2" | bc`
echo "CameraModel=$cameramodel, FocalLengthIn35mmFilm: $focallenght35 mm, F$fnumber, $exptime sec, ISO $isospeed"


相机型号=DMC-FZ30,FocalLengthIn35mmFilm:35 毫米,F2.8,0.125 秒,ISO 200

或者,

infile="P1050001.JPG"
declare `convert -ping "$infile" -format "cameramodel=%[EXIF:model]\n focallenght35=%[EXIF:FocalLengthIn35mmFilm]\n fnumber=%[EXIF:FNumber]\n exptime=%[EXIF:ExposureTime]\n isospeed=%[EXIF:ISOSpeedRatings]\n" info:`
fnumber1=`echo $fnumber | cut -d/ -f1`
fnumber2=`echo $fnumber | cut -d/ -f2`
fnumber=`echo "scale=1; $fnumber1/$fnumber2" | bc`
exptime1=`echo $exptime | cut -d/ -f1`
exptime2=`echo $exptime | cut -d/ -f2`
exptime=`echo "scale=0; $exptime2/$exptime1" | bc`
echo "CameraModel=$cameramodel, FocalLengthIn35mmFilm: $focallenght35 mm, F$fnumber, 1/$exptime sec, ISO $isospeed"


相机型号=DMC-FZ30,FocalLengthIn35mmFilm:35 毫米,F2.8,1/8 秒,ISO 200

于 2017-12-30T19:03:35.977 回答