【问题标题】:PowerShell - Reduces the size of imagesPowerShell - 减小图像的大小
【发布时间】:2017-01-27 16:55:07
【问题描述】:

我尝试制作一个脚本来减小文件夹中图像的大小。但我总是收到一条错误消息。 我必须减小同一文件夹和子文件夹中的大小

请您帮忙构建我的脚本吗?

提前谢谢你。

这是脚本:

$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

【问题讨论】:

    标签: powershell


    【解决方案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 也应该改变。

    【讨论】:

    • 感谢您的帮助。我可以减小图像 .png 的大小吗?修改脚本修改图片扩展名。
    • 对不起,是我,我在$source中出错了。
    • 我还有一个问题。我必须排除一些带有照片的文件夹,但参数 -exclude 不起作用。我该怎么办?
    • @pcarrey :运行get-help get-childitem -Parameter exclude 表明它接受a path element or pattern, such as "*.txt"。根据您在 exclude_list 中的内容,这可能是问题所在。
    【解决方案2】:

    使用 $source_photos.fullname 即

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

    【讨论】:

      猜你喜欢
      • 2017-02-06
      • 2019-04-13
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多