通过 ImageMagick 转换时的 Powershell 进度条。如何捕获-monitor 输出?

发布于 2025-01-11 15:03:59 字数 1506 浏览 4 评论 0原文

我有一个 PowerShell 脚本,可以简单地将任意数量的 PSD 文件转换为 PNG 文件。

代码如下:

param (
    [Parameter(Mandatory,Position = 0)]
    [String]
    $FileList,

    [Parameter(Mandatory)]
    [ValidateSet('png','jpg',IgnoreCase = $true)]
    [String]
    $DestFormat
)

$Files = Get-Content $FileList

$Files | ForEach-Object -Parallel {

    $MagickArgs = [System.Collections.ArrayList]@()
    $File       = $_

    $OrgFilePathOnly = [System.IO.Path]::GetDirectoryName($File)
    $OrgFilenameNoExtension = [System.IO.Path]::GetFileNameWithoutExtension($File)
    $OrgFilename = $OrgFilenameNoExtension + '.psd[0]'
    $OrgFile = $OrgFilePathOnly + '\' + $OrgFilename

    $NewFile = [System.IO.Path]::ChangeExtension($File, ($Using:DestFormat).ToLower())
    
    magick.exe -monitor $OrgFile $NewFile | Out-Null


} -ThrottleLimit 20

一切工作正常,但我希望能够在控制台窗口中为每次转换显示进度条。如果我将 -monitor 标志添加到 magick 调用中,我会得到如下输出:

save image[D:\2D\Test\Just Flow Dark 03.png]: 50 of 4500, 01% complete
save image[D:\2D\Test\Just Flow Dark 03.png]: 100 of 4500, 02% complete
save image[D:\2D\Test\Just Flow Dark 03.png]: 150 of 4500, 03% complete
save image[D:\2D\Test\Just Flow Dark 03.png]: 200 of 4500, 04% complete
save image[D:\2D\Test\Just Flow Dark 03.png]: 250 of 4500, 05% complete
save image[D:\2D\Test\Just Flow Dark 03.png]: 300 of 4500, 06% complete

等等。虽然我可能可以保留原样,但我想学习如何重定向或捕获此输出,以便我可以显示自定义进度条(winforms 或 Show-Progress / Write-Progress)。

任何帮助都会很棒。

谢谢!

I have a PowerShell script that simply converts any number of PSD files to PNG files.

Here's the code:

param (
    [Parameter(Mandatory,Position = 0)]
    [String]
    $FileList,

    [Parameter(Mandatory)]
    [ValidateSet('png','jpg',IgnoreCase = $true)]
    [String]
    $DestFormat
)

$Files = Get-Content $FileList

$Files | ForEach-Object -Parallel {

    $MagickArgs = [System.Collections.ArrayList]@()
    $File       = $_

    $OrgFilePathOnly = [System.IO.Path]::GetDirectoryName($File)
    $OrgFilenameNoExtension = [System.IO.Path]::GetFileNameWithoutExtension($File)
    $OrgFilename = $OrgFilenameNoExtension + '.psd[0]'
    $OrgFile = $OrgFilePathOnly + '\' + $OrgFilename

    $NewFile = [System.IO.Path]::ChangeExtension($File, ($Using:DestFormat).ToLower())
    
    magick.exe -monitor $OrgFile $NewFile | Out-Null


} -ThrottleLimit 20

Everything is working fine, but I want to be able to show a progress bar in the console window for each conversion. If I add the -monitor flag to the magick call, I get output like:

save image[D:\2D\Test\Just Flow Dark 03.png]: 50 of 4500, 01% complete
save image[D:\2D\Test\Just Flow Dark 03.png]: 100 of 4500, 02% complete
save image[D:\2D\Test\Just Flow Dark 03.png]: 150 of 4500, 03% complete
save image[D:\2D\Test\Just Flow Dark 03.png]: 200 of 4500, 04% complete
save image[D:\2D\Test\Just Flow Dark 03.png]: 250 of 4500, 05% complete
save image[D:\2D\Test\Just Flow Dark 03.png]: 300 of 4500, 06% complete

Etc. While I could probably just leave things like they are, I want to learn how to redirect or capture this output in such a way that I can display a custom progress bar (either winforms or Show-Progress / Write-Progress).

Any help at all would be great.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文