あまり写真とか撮らないのだけど、ふとLinuxのコンソール上で画像データのEXIF情報を取得するにはどうすれば良いのかなと思ったので調べてみたところ、ImageMagickを使ってやることで、コンソール上からファイルのEXIF情報を取得・削除といった事が可能なようだ。 ImageMagickは、CentOSならyum、Ubuntuであればaptからインストールができる。

sudo apt install imagemagick
sudo yum install ImageMagick

EXIF情報を確認する

インストール後、以下のコマンドでファイルのEXIF情報を取得することができる。 今回は、手元にEXIF情報のあるファイルがないのでこちらのサンプル画像を利用させてもらった。

identify -verbose ファイルPATH
[root@BS-PUB-CENT7-01 ~]# wget http://www.ksky.ne.jp/~yamama/jpggpsmap/sample/AkihabaraKousaten.JPG
--2017-08-06 04:43:36--  http://www.ksky.ne.jp/~yamama/jpggpsmap/sample/AkihabaraKousaten.JPG
www.ksky.ne.jp (www.ksky.ne.jp) をDNSに問いあわせています... 210.233.190.84
www.ksky.ne.jp (www.ksky.ne.jp)|210.233.190.84|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 548570 (536K) [image/jpeg]
`AkihabaraKousaten.JPG' に保存中

100%[===================================================================================================================================================================================================>] 548,570     --.-K/s 時間 0.05s

2017-08-06 04:43:36 (11.4 MB/s) - `AkihabaraKousaten.JPG' へ保存完了 [548570/548570]

[root@BS-PUB-CENT7-01 ~]# identify -verbose ./AkihabaraKousaten.JPG
Image: ./AkihabaraKousaten.JPG
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Class: DirectClass
  Geometry: 1200x1600+0+0
  Resolution: 72x72
  Print size: 16.6667x22.2222
  Units: PixelsPerInch
  Type: TrueColor
  Endianess: Undefined
  Colorspace: sRGB
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
  Channel statistics:
    Red:
      min: 0 (0)
      max: 255 (1)
      mean: 92.9754 (0.364609)
      standard deviation: 62.7977 (0.246266)
      kurtosis: 0.812823
      skewness: 1.30089
    Green:
      min: 0 (0)
      max: 255 (1)
      mean: 94.5935 (0.370955)
      standard deviation: 64.6191 (0.253408)
      kurtosis: 0.690189
      skewness: 1.36728
    Blue:
      min: 0 (0)
      max: 255 (1)
      mean: 104.32 (0.409098)
      standard deviation: 68.8523 (0.270009)
      kurtosis: -0.114506
      skewness: 1.09011
  Image statistics:
    Overall:
      min: 0 (0)
      max: 255 (1)
      mean: 97.2963 (0.381554)
      standard deviation: 65.4722 (0.256754)
      kurtosis: 0.464047
      skewness: 1.26142
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Interlace: None
  Background color: white
  Border color: srgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Compose: Over
  Page geometry: 1200x1600+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: JPEG
  Quality: 92
  Orientation: Undefined
  Properties:
    date:create: 2017-08-06T04:43:36+09:00
    date:modify: 2008-07-09T22:31:13+09:00
    exif:ApertureValue: 30/10
    exif:BrightnessValue: -1/-1
    exif:ColorSpace: 1
    exif:ComponentsConfiguration: 1, 2, 3, 0
    exif:Compression: 6
    exif:Contrast: 0
    exif:CustomRendered: 0
    exif:DateTime: 2008:07:04 21:01:52
    exif:DateTimeDigitized: 2008:07:04 21:01:52
    exif:DateTimeOriginal: 2008:07:04 21:01:52
    exif:DigitalZoomRatio: 100/100
    exif:ExifImageLength: 1600
    exif:ExifImageWidth: 1200
    exif:ExifOffset: 350
    exif:ExifVersion: 48, 50, 50, 48
    exif:ExposureMode: 0
    exif:Flash: 24
    exif:FlashPixVersion: 48, 49, 48, 48
    exif:FNumber: 30/10
    exif:FocalLength: 477/100
    exif:FocalLengthIn35mmFilm: 28
    exif:GPSInfo: 660
    exif:GPSLatitude: 35/1, 41/1, 5920/100
    exif:GPSLatitudeRef: N
    exif:GPSLongitude: 139/1, 46/1, 1812/100
    exif:GPSLongitudeRef: E
    exif:GPSMapDatum: WGS-84
    exif:GPSVersionID: 2, 2, 0, 0
    exif:ImageDescription: SA390017
    exif:JPEGInterchangeFormat: 888
    exif:JPEGInterchangeFormatLength: 6623
    exif:LightSource: 0
    exif:Make: KDDI-SA
    exif:MaxApertureValue: 30/10
    exif:MeteringMode: 1
    exif:Model: W51SA
    exif:ResolutionUnit: 2
    exif:Saturation: 0
    exif:SceneCaptureType: 0
    exif:SceneType: 1
    exif:Sharpness: 0
    exif:SubjectDistance: 0/0
    exif:SubjectDistanceRange: 0
    exif:WhiteBalance: 0
    exif:XResolution: 72/1
    exif:YCbCrPositioning: 1
    exif:YResolution: 72/1
    jpeg:colorspace: 2
    jpeg:sampling-factor: 1x2,1x1,1x1
    signature: ed4f58a3429273fc4d64fe40d616bf77b0805723db5170a16684fafba37f8fb3
    unknown: 65, 83, 67, 73, 73, 0, 0, 0, 71, 80, 83, 45, 70, 73, 88
  Profiles:
    Profile-exif: 7517 bytes
  Artifacts:
    filename: ./AkihabaraKousaten.JPG
    verbose: true
  Tainted: False
  Filesize: 549KB
  Number pixels: 1.92M
  Pixels per second: 64MB
  User time: 0.040u
  Elapsed time: 0:01.030
  Version: ImageMagick 6.7.8-9 2016-06-16 Q16 http://www.imagemagick.org
[root@BS-PUB-CENT7-01 ~]# identify -verbose ./AkihabaraKousaten.JPG | grep 'exif:'
    exif:ApertureValue: 30/10
    exif:BrightnessValue: -1/-1
    exif:ColorSpace: 1
    exif:ComponentsConfiguration: 1, 2, 3, 0
    exif:Compression: 6
    exif:Contrast: 0
    exif:CustomRendered: 0
    exif:DateTime: 2008:07:04 21:01:52
    exif:DateTimeDigitized: 2008:07:04 21:01:52
    exif:DateTimeOriginal: 2008:07:04 21:01:52
    exif:DigitalZoomRatio: 100/100
    exif:ExifImageLength: 1600
    exif:ExifImageWidth: 1200
    exif:ExifOffset: 350
    exif:ExifVersion: 48, 50, 50, 48
    exif:ExposureMode: 0
    exif:Flash: 24
    exif:FlashPixVersion: 48, 49, 48, 48
    exif:FNumber: 30/10
    exif:FocalLength: 477/100
    exif:FocalLengthIn35mmFilm: 28
    exif:GPSInfo: 660
    exif:GPSLatitude: 35/1, 41/1, 5920/100
    exif:GPSLatitudeRef: N
    exif:GPSLongitude: 139/1, 46/1, 1812/100
    exif:GPSLongitudeRef: E
    exif:GPSMapDatum: WGS-84
    exif:GPSVersionID: 2, 2, 0, 0
    exif:ImageDescription: SA390017
    exif:JPEGInterchangeFormat: 888
    exif:JPEGInterchangeFormatLength: 6623
    exif:LightSource: 0
    exif:Make: KDDI-SA
    exif:MaxApertureValue: 30/10
    exif:MeteringMode: 1
    exif:Model: W51SA
    exif:ResolutionUnit: 2
    exif:Saturation: 0
    exif:SceneCaptureType: 0
    exif:SceneType: 1
    exif:Sharpness: 0
    exif:SubjectDistance: 0/0
    exif:SubjectDistanceRange: 0
    exif:WhiteBalance: 0
    exif:XResolution: 72/1
    exif:YCbCrPositioning: 1
    exif:YResolution: 72/1
    Profile-exif: 7517 bytes

上記コマンドだと大量のデータが取得されるため、必要な箇所(カメラなどのモデルや緯度、経度といったGPS情報)のみを表示させる場合は、以下のようにformatを指定してやればいい。

identify -format "Make:%[EXIF:Make]\nModel:%[EXIF:Model]\nDate:%[EXIF:DateTime]\n緯度:%[EXIF:GPSLatitude]\n経度:%[EXIF:GPSLongitude]" ファイルPATH
[root@BS-PUB-CENT7-01 ~]# identify -format "Make:%[EXIF:Make]\nModel:%[EXIF:Model]\nDate:%[EXIF:DateTime]\n緯度:%[EXIF:GPSLatitude]\n経度:%[EXIF:GPSLongitude]" ./AkihabaraKousaten.JPG
Make:KDDI-SA
Model:W51SA
Date:2008:07:04 21:01:52
緯度:35/1, 41/1, 5920/100
経度:139/1, 46/1, 1812/100

EXIF情報を削除する

EXIF情報を削除する場合、convertコマンドで別のファイルに書き出すか、もしくはmogrifyコマンドで直接ファイルを変更することが可能だ。

convert -strip 元ファイル コピー先ファイル
[root@BS-PUB-CENT7-01 ~]# convert -strip ./AkihabaraKousaten.JPG ./AkihabaraKousaten.JPG.strip
[root@BS-PUB-CENT7-01 ~]# identify -verbose ./AkihabaraKousaten.JPG.strip
Image: ./AkihabaraKousaten.JPG.strip
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Class: DirectClass
  Geometry: 1200x1600+0+0
  Resolution: 72x72
  Print size: 16.6667x22.2222
  Units: PixelsPerInch
  Type: TrueColor
  Endianess: Undefined
  Colorspace: sRGB
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
  Channel statistics:
    Red:
      min: 0 (0)
      max: 255 (1)
      mean: 92.9628 (0.36456)
      standard deviation: 62.8308 (0.246395)
      kurtosis: 0.809192
      skewness: 1.29937
    Green:
      min: 0 (0)
      max: 255 (1)
      mean: 94.58 (0.370902)
      standard deviation: 64.61 (0.253373)
      kurtosis: 0.688694
      skewness: 1.36622
    Blue:
      min: 0 (0)
      max: 255 (1)
      mean: 104.273 (0.408912)
      standard deviation: 68.8235 (0.269896)
      kurtosis: -0.116854
      skewness: 1.08713
  Image statistics:
    Overall:
      min: 0 (0)
      max: 255 (1)
      mean: 97.2718 (0.381458)
      standard deviation: 65.4697 (0.256744)
      kurtosis: 0.460821
      skewness: 1.2593
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Interlace: None
  Background color: white
  Border color: srgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Compose: Over
  Page geometry: 1200x1600+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: JPEG
  Quality: 92
  Orientation: Undefined
  Properties:
    date:create: 2017-08-06T05:12:41+09:00
    date:modify: 2017-08-06T05:12:41+09:00
    jpeg:colorspace: 2
    jpeg:sampling-factor: 1x2,1x1,1x1
    signature: ac1086147cdc50ba6dede52f12f1c135a26da23717bf44955166f8274634ef28
  Artifacts:
    filename: ./AkihabaraKousaten.JPG.strip
    verbose: true
  Tainted: False
  Filesize: 434KB
  Number pixels: 1.92M
  Pixels per second: 48MB
  User time: 0.040u
  Elapsed time: 0:01.039
  Version: ImageMagick 6.7.8-9 2016-06-16 Q16 http://www.imagemagick.org
[root@BS-PUB-CENT7-01 ~]# mogrify -strip ./AkihabaraKousaten.JPG
[root@BS-PUB-CENT7-01 ~]# identify -verbose ./AkihabaraKousaten.JPG
Image: ./AkihabaraKousaten.JPG
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Class: DirectClass
  Geometry: 1200x1600+0+0
  Resolution: 72x72
  Print size: 16.6667x22.2222
  Units: PixelsPerInch
  Type: TrueColor
  Endianess: Undefined
  Colorspace: sRGB
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
  Channel statistics:
    Red:
      min: 0 (0)
      max: 255 (1)
      mean: 92.9628 (0.36456)
      standard deviation: 62.8308 (0.246395)
      kurtosis: 0.809192
      skewness: 1.29937
    Green:
      min: 0 (0)
      max: 255 (1)
      mean: 94.58 (0.370902)
      standard deviation: 64.61 (0.253373)
      kurtosis: 0.688694
      skewness: 1.36622
    Blue:
      min: 0 (0)
      max: 255 (1)
      mean: 104.273 (0.408912)
      standard deviation: 68.8235 (0.269896)
      kurtosis: -0.116854
      skewness: 1.08713
  Image statistics:
    Overall:
      min: 0 (0)
      max: 255 (1)
      mean: 97.2718 (0.381458)
      standard deviation: 65.4697 (0.256744)
      kurtosis: 0.460821
      skewness: 1.2593
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Interlace: None
  Background color: white
  Border color: srgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Compose: Over
  Page geometry: 1200x1600+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: JPEG
  Quality: 92
  Orientation: Undefined
  Properties:
    date:create: 2017-08-06T05:19:13+09:00
    date:modify: 2017-08-06T05:19:13+09:00
    jpeg:colorspace: 2
    jpeg:sampling-factor: 1x2,1x1,1x1
    signature: ac1086147cdc50ba6dede52f12f1c135a26da23717bf44955166f8274634ef28
  Artifacts:
    filename: ./AkihabaraKousaten.JPG
    verbose: true
  Tainted: False
  Filesize: 434KB
  Number pixels: 1.92M
  Pixels per second: 64MB
  User time: 0.020u
  Elapsed time: 0:01.029
  Version: ImageMagick 6.7.8-9 2016-06-16 Q16 http://www.imagemagick.org