【问题标题】:Powershell read metadata of large filesPowershell读取大文件的元数据
【发布时间】:2012-07-10 22:43:33
【问题描述】:

我有一个脚本,它循环播放我拍摄的大量图像并读取焦距和相机模型,呈现焦距和总焦距图表(这有助于确定下一次购买镜头,但这不是重点)。

这对于 10 MB 以下的 JPG 图像非常有效,但一旦遇到接近 20 MB 的 RAW 文件(如佳能的 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 文件只会在任何属性上返回空白...

非常感谢任何帮助!

【问题讨论】:

    标签: powershell metadata


    【解决方案1】:

    问题是发生错误时图像对象没有被释放。发生错误时执行退出 try 块,这意味着永远不会执行 Dispose 调用并且永远不会返回内存。

    要解决此问题,您必须将 $image.Dispose() 调用放在 try/catch 末尾的 finally 块中。像这样

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

    【讨论】:

    • 不幸的是,在添加了 finally 块后,我仍然收到以下异常;使用“1”参数调用“FromFile”的异常:“内存不足”。在 C:\Users\Alexander\Documents\Powershell\get-Focallengths.ps1:36 char:57 + $image = [System.Drawing.Imaging.Metafile]::FromFile
    【解决方案2】:

    我使用this module 获取图像上的 EXIF 数据。 我从未在 .CR2 上测试过它,但在 .CRW 上测试过大约 15MB。

    试试看,然后告诉我。

    【讨论】:

    • 不幸的是,该模块不适用于 .CR2 文件 :-( Get-EXIF 只会返回大量空白条目,这很烦人。回到绘图板!
    猜你喜欢
    • 2019-10-23
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    相关资源
    最近更新 更多