在Powershell中将图像转换为BMP 16位

wi3ka0sx  于 2023-05-17  发布在  Shell
关注(0)|答案(1)|浏览(133)

我有一个代码批量转换为BMP的JPG文件夹。
我的代码工作正常,但它被保存为BMP 24位。
我需要它转换为BMP 16位使用PowerShell

function ConvertImage{

$Origen="C:\jpg" #path to files
$Destino="C:\bmp" #path to files

if (Test-Path $Origen)
{
#Load required assemblies and get object reference
 [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
 foreach($file in (Get-ChildItem -Path "$Origen\*.jpg" -Exclude *-*)){
     $convertfile = new-object System.Drawing.Bitmap($file.Fullname)
     $newfilname = $Destino + '\' + $file.BaseName + '.bmp'
     $convertfile.Save($newfilname, "bmp")
     $file.Fullname
    }  
 }
    else
 {
    Write-Host "Path not found."
 }
};ConvertImage -path $args[0]
332nm8kg

332nm8kg1#

我修改了你的脚本从JPG转换为8位BMP:

function ConvertImage {

$Origen = "D:\JPG"  #path to input files
$Destino = "D:\BMP"  #path to output files

if (Test-Path $Origen) {
    
    [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
    foreach ($file in (Get-ChildItem -Path "$Origen\*.jpg" -Exclude *-*)) {
        
        $convertFile = New-Object System.Drawing.Bitmap($file.FullName)
        $format = [System.Drawing.Imaging.ImageFormat]::Bmp
        $newFileName = $Destino + '\' + $file.BaseName + '.bmp'
        $newFile = $convertFile.Clone([System.Drawing.Rectangle]::FromLTRB(0, 0, $convertFile.Width, $convertFile.Height), [System.Drawing.Imaging.PixelFormat]::Format8bppIndexed)
        $newFile.Save($newFileName, $format)
        $file.FullName
    }
} else {
    Write-Host "Path not found."
}
}
ConvertImage

您可以将Format8bppIndexed更改为this page上列出的其他格式之一

相关问题