通过Powershell进行智能图像搜索 [英] Smart image search via Powershell
问题描述
我对通过自定义属性进行文件搜索感兴趣。例如,我想找到具有特定尺寸的所有JPEG图像。看起来像是
I am interested in file searching by custom properties. For example, I want to find all JPEG-images with certain dimensions. Something looks like
Get-ChildItem -Path C:\ -Filter *.jpg -Recursive | where-object { $_.Dimension -eq '1024x768' }
我怀疑它是关于系统的使用。画画。怎么做?
提前致谢
I suspect it's about using of System.Drawing. How it can be done? Thanks in advance
推荐答案
这实际上很容易做到,你对System.Drawing的直觉实际上是正确的:
That's actually pretty easy to do and your gut feeling about System.Drawing was in fact correct:
Add-Type -Assembly System.Drawing
$input | ForEach-Object { [Drawing.Image]::FromFile($_) }
将其另存为 Get-Image.ps1
在您的路径中的某个地方,然后您就可以使用它。
Save that as Get-Image.ps1
somewhere in your path and then you can use it.
另一种选择是添加跟随 $ profile
:
Another option would be to add the following to your $profile
:
Add-Type -Assembly System.Drawing
function Get-Image {
$input | ForEach-Object { [Drawing.Image]::FromFile($_) }
}
其工作方式基本相同。当然,在你认为合适的时候添加像文档这样的花哨的东西。
which works pretty much the same. Of course, add fancy things like documentation or so as you see fit.
然后你就可以使用了:
gci -inc *.jpg -rec | Get-Image | ? { $_.Width -eq 1024 -and $_.Height -eq 768 }
请注意应该在使用它们之后处理以这种方式创建的对象。
Note that you should dispose the objects created this way after using them.
当然,你可以添加一个自定义的 Dimension
属性,这样你可以过滤:
Of course, you can add a custom Dimension
property so you could filter for that:
function Get-Image {
$input |
ForEach-Object { [Drawing.Image]::FromFile($_) } |
ForEach-Object {
$_ | Add-Member -PassThru NoteProperty Dimension ('{0}x{1}' -f $_.Width,$_.Height)
}
}
这篇关于通过Powershell进行智能图像搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!