2017-01-26 222 views
1

我尝试制作一个脚本,以减小文件夹中图像的大小。但我总是有一个错误信息。 我必须在同一个文件夹和子文件夹中缩小文件大小PowerShell - 减小图像大小

您能帮我建立我的脚本吗?

预先感谢您。

下面是脚本:

$source = "U:\TEST\Compression\images" 
$exclude_list = "(Imprimerie|Photos)" 

$source_listephotos = Get-ChildItem $source -Recurse | where {$_.FullName -notmatch $exclude_list} 

foreach ($source_photos in $source_listephotos) { 

$source_photos 

Resize-Image -InputFile $source_photos.FullName -Scale 30 -OutputFile (Join-Path $source $source_photos.Name) -Verbose 

} 

而且这里的错误消息:

Exception calling "Save" with "1" argument(s): "A generic error occurred in GDI+." 
At C:\windows\system32\windowspowershell\v1.0\Modules\Resize-Image\Resize-Image.psm1:70 char:9 
+   $img2.Save($OutputFile); 
+   ~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : ExternalException 

回答

1

您正在使用(我认为)的Resize-Image模块。

报告的问题是路径的文件格式不受支持。

没有进一步的数据,我假设你将一个完整的对象传递给OutputFile。要解决此问题,请尝试指定Name属性。

$source = "U:\TEST\Compression\images" 
$destination = "U:\TEST\Compression\image_resizer" 
$exclude_list = @("*Imprimerie","*Photos*") 

$source_listephotos = Get-ChildItem $source -Exclude $exclude_list -Recurse 

foreach ($source_photos in $source_listephotos) { 
    Resize-Image -InputFile $source_photos.FullName -Scale 30 -OutputFile (Join-Path $destination $source_photos.Name) -Verbose 
} 

正如其他答案所述,InputFile也应该改变。

+0

谢谢您的帮助。 我可以缩小图像的大小吗?修改脚本修改图片扩展名。 – pcarrey

+0

对不起,这是我,我在$ source中犯了一个错误。 – pcarrey

+0

我有其他问题。我必须排除一些带有照片的文件夹,但参数-exclude不起作用。我该怎么办? – pcarrey

0

使用$ source_photos.fullname即

... Resize-Image -InputFile $($source_photos.fullname) -Scale .... 
+0

谢谢您的回答 – pcarrey