powershellbitmapgdi+system.drawingimage-conversion

In Powershell convert image to BMP 16 bits


I have a code to batch convert a folder of jpg files to BMP.

The code i have works OK, but it gets saved as BMP 24 bits.

I need it to convert it to BMP 16 bits using 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]

Solution

  • I modified your script to convert from JPG to 8-bit 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 
    

    You can change the Format8bppIndexed to one of the other formats listed on this page