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

发布于 2025-01-11 06:24:10 字数 731 浏览 5 评论 0原文

我有一个代码可以将 jpg 文件文件夹批量转换为 BMP。

我的代码工作正常,但它被保存为 BMP 24 位。

我需要它使用 Powershell 将其转换为 BMP 16 位

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]

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]

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-01-18 06:24:10

我修改了您的脚本以从 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 更改为 此页面

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文