【问题标题】:Converting transparent png to jpg powershell将透明png转换为jpg powershell
【发布时间】:2017-12-01 23:03:16
【问题描述】:

我正在尝试将一堆透明的 png 批量转换为 jpg 并且下面的鹅卵石 powershell 可以工作,但是每当我转换所有图像时都会变成黑色。我在这里尝试了答案 Convert Transparent PNG to JPG with Non-Black Background Color 但我得到“使用”关键字不受支持(在模块中也找不到)

$files = Get-ChildItem "C:\Pictures\test" -Filter *.png -file -Recurse | 
foreach-object {

    $Source = $_.FullName
    $test = [System.IO.Path]::GetDirectoryName($source)
    $base= $_.BaseName+".jpg"
    $basedir = $test+"\"+$base
    Write-Host $basedir
    Add-Type -AssemblyName system.drawing
    $imageFormat = "System.Drawing.Imaging.ImageFormat" -as [type]
    $image = [drawing.image]::FromFile($Source)
    $image.Save($basedir, $imageFormat::jpeg)
}  

据我了解,您需要创建一个具有白色背景的新位图图形并在其上绘制此图像,但对于我来说,我无法弄清楚如何添加它。

【问题讨论】:

标签: powershell


【解决方案1】:

根据Convert Transparent PNG to JPG with Non-Black Background Color的回答

$files = Get-ChildItem "C:\Pictures\test" -Filter *.png -file -Recurse | 
foreach-object {

    $Source = $_.FullName
    $test = [System.IO.Path]::GetDirectoryName($source)
    $base= $_.BaseName+".jpg"
    $basedir = $test+"\"+$base
    Write-Host $basedir
    Add-Type -AssemblyName system.drawing
    $imageFormat = "System.Drawing.Imaging.ImageFormat" -as [type]
    $image = [drawing.image]::FromFile($Source)
    # $image.Save($basedir, $imageFormat::jpeg) Don't save here!

    # Create a new image
    $NewImage = [System.Drawing.Bitmap]::new($Image.Width,$Image.Height)
    $NewImage.SetResolution($Image.HorizontalResolution,$Image.VerticalResolution)

    # Add graphics based on the new image
    $Graphics = [System.Drawing.Graphics]::FromImage($NewImage)
    $Graphics.Clear([System.Drawing.Color]::White) # Set the color to white
    $Graphics.DrawImageUnscaled($image,0,0) # Add the contents of $image

    # Now save the $NewImage instead of $image
    $NewImage.Save($basedir,$imageFormat::Jpeg)
}  

【讨论】:

  • 您查看的帖子是using c#。这是同一件事,但在 PowerShell 中调用相关的类和对象。
猜你喜欢
  • 2018-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多