2016年11月5日土曜日

VBA から WIA Object を使って Exif 情報を取得

GPS 付きのデジカメで撮った JPG 写真に付加されている Exif 情報を取り出したいと思って、最近手を出し始めた Python を使うことを考えたのですが、これは仕事上必要な処理になるので、不慣れな Python で不安定なプログラムを書くより、使い慣れた VBA でどうにかならんかな?と考えていたら、こんな素晴らしいものがあることが分かりました。

WIA Object
Access(or Excel) で参照設定すれば使えます。
参照するライブラリの名前は Microsoft Windows Image Aquisition Library です。
私の環境には、 V2.0 が入ってました。( Office 2010 の Access14)

Sub GetExif()
  Dim ObjWia As Object 'WIA.ImageFile
  Set ObjWia = CreateObject("Wia.ImageFile")
    ObjWia.LoadFile "C:\Users\USER\Downloads\P12345.JPG"  '対象の写真ファイル

  On Error Resume Next
  For Each p In ObjWia.Properties
    i = i + 1
    V_ID = p.PropertyID
    V_Name = p.Name

    If p.PropertyID = 2 Or p.PropertyID = 4 Then
      V_Value = p.Value(1) + p.Value(2) / 60 + p.Value(3) / 3600  'ID=2=緯度 ID=4=経度
    Else
      V_Value = p.Value
    End If
   
    Debug.Print V_ID & " " & V_Name & " " & V_Value
   
  Next
  On Error GoTo 0
 
  Set ObjWia = Nothing
End Sub


イミディエイトウィンドウ
----------------------------------------------------------
PropertyID PropertyName PropertyValue
271 EquipMake Panasonic
272 EquipModel DMC-TZ40
274 Orientation 1
282 XResolution 180
283 YResolution 180
296 ResolutionUnit 2
305 SoftwareUsed PPP
306 DateTime 2016:11:06 11:59:33
315 Artist
531 YCbCrPositioning 2
33434 ExifExposureTime 0.025
33437 ExifFNumber 3.3
34850 ExifExposureProg 2
34855 ExifISOSpeed 400
34864 34864 1
36864 ExifVer
36867 ExifDTOrig 2016:11:06 11:59:33
36868 ExifDTDigitized 2016:11:06 11:59:33
37121 ExifCompConfig
37122 ExifCompBPP 4
37380 ExifExposureBias 0
37381 ExifMaxAperture 3.4453125
37383 ExifMeteringMode 5
37384 ExifLightSource 0
37385 ExifFlash 16
37386 ExifFocalLength 4.3
37500 ExifMakerNote
40960 ExifFPXVer
40961 ExifColorSpace 1
40962 ExifPixXDim 4896
40963 ExifPixYDim 2752
20545 20545 R98
20546 20546
41495 ExifSensingMethod 2
41728 ExifFileSource 3
41729 ExifSceneType 1
41985 41985 0
41986 41986 0
41987 41987 0
41988 41988 0
41989 41989 27
41990 41990 0
41991 41991 2
41992 41992 0
41993 41993 0
41994 41994 0
42016 42016 e97cd362db94a78a0000000000000000
0 GpsVer
1 GpsLatitudeRef N
2 GpsLatitude 36.0319055555556
3 GpsLongitudeRef E
4 GpsLongitude 138.210838888889
5 GpsAltitudeRef 0
6 GpsAltitude 0
7 GpsGpsTime
9 GpsGpsStatus A
10 GpsGpsMeasureMode 2
11 GpsGpsDop 1.9
16 GpsImgDirRef T
17 GpsImgDir 45
18 GpsMapDatum WGS-84  
27 27
28 28
29 29 2016:10:22
50341 50341
50898 50898
50899 50899
20507 ThumbnailData
20515 ThumbnailCompression 6
20525 ThumbnailResolutionX 72
20526 ThumbnailResolutionY 72
20528 ThumbnailResolutionUnit 2
513 JPEGInterFormat 5556
514 JPEGInterLength 7219
20625 ChrominanceTable
20624 LuminanceTable
----------------------------------------------------------

元々 GPS 情報が取得できているかどうかの判定に使用したくて探していたんですが、これがあれば、撮影日時やシャッタースピードなどの撮影情報も全部把握できます。
夢が広がりました。

0 件のコメント:

コメントを投稿