2012-07-10 37 views
0

我有一個腳本,通過我已經拍攝的圖像負載循環讀取焦距&攝像機模型,呈現焦距和總計圖(這非常適合幫助確定下一個鏡頭購買,但除此之外)。PowerShell讀大文件的元數據

這對於10 MB以下的JPG圖像來說絕對正常,但只要它碰到一個接近20 MB的RAW文件(如Canon的CR2格式),它就會吐出「內存不足」錯誤。

有沒有辦法增加Powershell中的內存限制,或只是讀取文件的元數據而不加載整個文件..?

這是我目前使用:

# load image by statically calling a method from .NET 
$image = [System.Drawing.Imaging.Metafile]::FromFile($file.FullName) 

# try to get the ExIf data (silently fail if the data can't be found) 
# http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/EXIF.html 
try 
{ 
    # get the Focal Length from the Metadata code 37386 
    $focalLength = $image.GetPropertyItem(37386).Value[0] 
    # get model data from the Metadata code 272 
    $modelByte = $image.GetPropertyItem(272) 
    # convert the model data to a String from a Byte Array 
    $imageModel = $Encode.GetString($modelByte.Value) 
    # unload image 
    $image.Dispose() 
} 
catch 
{ 
    #do nothing with the catch 
} 

我在這裏使用該解決方案的嘗試:http://goo.gl/WY7Rg但CR2文件只是返回的任何財產上的空白...

任何幫助,不勝感激!

回答

0

我使用this module獲取圖像上的EXIF數據。 我從來沒有在.CR2上測試它,但是在.CRW上測試了大約15MB。

試試吧,讓我知道。

+0

不幸的模塊不會對.CR2文件:-( 獲取-EXIF剛剛返回空白項的負載是煩人的工作。回到繪圖板! – Alex 2012-07-15 16:24:46

5

問題在於發生錯誤時圖像對象不會被丟棄。當發生錯誤時執行退出try塊,這意味着Dispose調用從不執行,並且內存永遠不會返回。

要解決此問題,您必須在您的try/catch結束時將$image.Dispose()呼叫放入finally塊中。像這樣

try 
{ 
    /* ... */ 
} 
catch 
{ 
    #do nothing with the catch 
} 
finally 
{ 
    # ensure image is always unloaded by placing this code in a finally block 
    $image.Dispose() 
} 
+0

不幸的是我還收到以下在添加Finally塊後出現異常; 異常情況下調用帶有「1」參數的「FromFile」:「內存不足」 在C:\ Users \ Alexander \ Documents \ Powershell \ get-Focallengths.ps1:36 char :57 + $ image = [System.Drawing.Imaging.Metafile] :: FromFile <<<<($ file.FullName) + CategoryInfo:NotSpecified:(:) [],MethodInvocationException + FullyQualifiedE rrorId:DotNetMethodException – Alex 2013-03-09 17:10:51