Powershell脚本将TIFF图像转换为重命名的JPEG
问题描述:
我需要将批量的TIFF图像转换为JPEG。它可以用普通的香草PowerShell来完成,无需安装ImageMagick?Powershell脚本将TIFF图像转换为重命名的JPEG
经过一番研究here和here,似乎可以做到。首先,通过加载.NET Framework程序集:
[Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”);
但因为我节省了转换后的图像在不同的目录,删除了文件名称的前四个字符,以及更改扩展到JPG,我与语法:
Get-ChildItem *.tif | %{ $file = new-object System.Drawing.Bitmap($_.FullName); $file.Save({ Join-Path "C:\Users\oscar\Downloads\" ($_.Name -replace '^.{4}(.*?)', '$1.jpg') },"JPEG") }
我得到一个“无效字符的路径名称”错误。
答
我不确定您的问题是位于转换还是创建新的文件名。如果文件名称创建是问题,您可以尝试以下步骤。例如:
PS C:\temp> "test.txt" -replace "txt", "somethingOther"
在你的情况:
Get-ChildItem -Include *.tif | %{ $file = new-object System.Drawing.Bitmap($_.FullName); $file.Save((Join-Path "C:\Users\oscar\Downloads\" ($_.Name -replace "tif", 'jpg')),"JPEG") }
我也换成大括号通过正常的一个(在Join-Path
)。
希望有帮助
+0
正如你和@TessellatingHeckler指出的那样,大括号需要用括号来代替。仍然需要修改重命名语法,但转换已经完成。 – oavaldezi
答
我不喜欢一个衬垫,而解释的东西。
该脚本包括为新的文件名需要.substring(4)
:
[void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$DestDir = "C:\Users\oscar\Downloads\"
$SrcDir = "X:\test\path"
PushD $SrcDir
Get-ChildItem *.tif |
ForEach-Object{
$NewFileName = (Join-Path $DestDir ($_.BaseName.SubString(4)+'.jpg'))
$Picture = new-object System.Drawing.Bitmap($_.FullName)
$Picture.Save($NewFileName ,"JPEG")
}
PopD
'.Save({这里随机码}, “JPEG”)'是传递一个脚本块'{}'的功能,而不是文件名。这些大括号需要加括号。 '$ file.Save((Join-Path“C:\ Users \ oscar \ Downloads \”($ _。Name -replace'^。{4}(。*?)','$ 1.jpg')),“ JPEG“) – TessellatingHeckler