使用System.Drawing.Image导出-Csv与$ imageFile.Width和$ _。全名

时间:2012-03-13 05:38:57

标签: image-processing powershell powershell-remoting export-to-csv system.drawing.imaging

我想知道是否有更好的方法来编写此脚本来收集图像尺寸和文件路径。该脚本适用于中小型目录,但我并不认为可能有100,000多个文件/文件夹。

Measure-Command {

[Void][System.Reflection.Assembly]::LoadFile( "C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll")

$path = "\\servername.corp.company.com\top_directory"

$data = Get-ChildItem -Recurse $path | % {
 $imageFile = [System.Drawing.Image]::FromFile($_.FullName) ;
 New-Object PSObject -Property @{
 name = $_.Name
 fullname = $_.Fullname
 width = $imageFile.Width
 height = $imageFile.Height
 length = $_.Length
 }
 }
 $data | Where-Object {$_.width -eq 500 -or $_.width -eq 250 -or $_.width -eq 1250  } |

Export-Csv \\servername.corp.company.com\top_directory\some_directory\log_file.csv -NoTypeInformation } 

我现在实际上并没有使用Where-Object过滤器。

使用appx在远程目录上运行上述脚本时。脚本占用appx的20,000个文件+文件夹。 26分钟,然后创建.csv。

我在Windows 7上运行Powershell V2 ISE的脚本,我相信远程服务器在Windows Server 2003上。

直接从远程服务器运行脚本会更快吗?

由于所有数据都是在写入csv之前在“缓存”中收集的,所以进行csv的过程是否会缓慢?

如果所有我必须经历的是20,000个文件,我会等待26分钟,但是500,000个文件和文件夹是漫长的等待。

我正在测试以下方法,因为我认为我的真正问题不是速度,而是在内存中存储了太多数据。感谢George Howarth的一篇文章,感谢PoSherLife的顶级剧本-http://powershellcommunity.org/tabid/54/aft/4844/Default.aspx

Measure-Command {
[System.Reflection.Assembly]::LoadFile( "C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll") 

"Name|SizeInBytes|Width|Height|FullName" >> C:\Users\fcool\Documents\JPGInfo.txt  

$path = "C:\Users\fcool\Documents" 
$images = Get-ChildItem -Recurse $path -Include *.jpg 

foreach ($image in $images) 
{ 
$name = $image.Name 
$length = $image.Length 
$imageFile = [System.Drawing.Image]::FromFile($image.FullName) 
$width = $imageFile.Width 
$height = $imageFile.Height
$FullName = $image.Fullname 

"$name|$length|$width|$height|$FullName" >> C:\Users\fcool\Documents\JPGInfo.txt  

$imageFile.Dispose() 
}
}

在非图像文件类型上运行这些脚本时是否存在任何风险/性能损失?

当我不排除非图像时,我收到此错误:

Exception calling "FromFile" with "1" argument(s): "Out of memory." 
At C:\scripts\directory_contents_IMAGE_DIMS_ALT_method.ps1:13 char:46 
+ $imageFile = [System.Drawing.Image]::FromFile <<<< ($image.FullName) 
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException 
+ FullyQualifiedErrorId : DotNetMethodException 

感谢您的任何建议! 再次感谢George Howarth和PoSherLife的剧本!

1 个答案:

答案 0 :(得分:1)

-FilterGet-ChildItem一起使用比-Include快得多,但您只能应用一个过滤字符串。因此,如果您只想匹配* .jpg,则可以使用过滤器。在我的测试中使用过滤器的速度比包含过快了近5倍。

Get-ChildItem -Recurse \\server\Photos -Filter *.jpg | % {
    $image = [System.Drawing.Image]::FromFile($_.FullName)
    if ($image.width -eq 500 -or $image.width -eq 250 -or $image.width -eq 1250) {
        New-Object PSObject -Property @{
            name = $_.Name
            fullname = $_.Fullname
            width = $image.Width
            height = $image.Height
            length = $_.Length
        }
    }
} | Export-Csv 'C:\log.csv' -NoTypeInformation