如果图片大于,如何调整图片大小

时间:2017-08-02 06:34:25

标签: powershell image-resizing

我这里有这个脚本来检查图片是否大于1024x768。然后我如何将这些图片调整为1024x768?

function Get-Image{ 
process {
          $file = $_
          [Drawing.Image]::FromFile($_.FullName)  |
          ForEach-Object{           
            $_ | Add-Member -PassThru NoteProperty FullName ('{0}' -f $file.FullName)
          }
         }
}    
Get-ChildItem -Path 'E:\Parts' -Filter *.jpg -Recurse | Get-Image | ? { $_.Width -gt 1024 -or $_.Height -gt 768 } | select -expa Fullname | get-item

1 个答案:

答案 0 :(得分:2)

您可以使用WIA(Windows Image Acquisition):

$wia = New-Object -com wia.imagefile
$wia.LoadFile("d:\tobi.jpg")
$wip = New-Object -ComObject wia.imageprocess
$scale = $wip.FilterInfos.Item("Scale").FilterId                    
$wip.Filters.Add($scale)
$wip.Filters[1].Properties("MaximumWidth") = 1024
$wip.Filters[1].Properties("MaximumHeight") = 768
#aspect ratio should be set as false if you want the pics in exact size 
$wip.Filters[1].Properties("PreserveAspectRatio") = $false
$wip.Apply($wia) 
$newimg = $wip.Apply($wia)
$newimg.SaveFile("d:\tobi0.jpg")