通过 ImageMagick 转换时的 Powershell 进度条。如何捕获-monitor 输出?
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论