Convert all images to jpg

后端 未结 2 1663
深忆病人
深忆病人 2021-01-01 00:09

I need to convert all images in folder and subfolders to jpg. I need to batch this process with command file. GUI tools needles for me I need the script.

I tried to

2条回答
  •  孤城傲影
    2021-01-01 00:58

    I adapted Chad Miller's answer to incorporate the ability to set JPEG's quality level from this blog post:
    Benoît Patra's blog: Resize image and preserve ratio with Powershell

    # Try uncommenting the following line if you receive errors about a missing assembly
    # [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    function ConvertTo-Jpg
    {
      [cmdletbinding()]
      param([Parameter(Mandatory=$true, ValueFromPipeline = $true)] $Path)
    
      process{
        $qualityEncoder = [System.Drawing.Imaging.Encoder]::Quality
        $encoderParams = New-Object System.Drawing.Imaging.EncoderParameters(1)
    
        # Set JPEG quality level here: 0 - 100 (inclusive bounds)
        $encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter($qualityEncoder, 100)
        $jpegCodecInfo = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | where {$_.MimeType -eq 'image/jpeg'}
    
        if ($Path -is [string]) {
          $Path = get-childitem $Path
        }
    
        $Path | foreach {
          $image = [System.Drawing.Image]::FromFile($($_.FullName))
          $filePath =  "{0}\{1}.jpg" -f $($_.DirectoryName), $($_.BaseName)
          $image.Save($filePath, $jpegCodecInfo, $encoderParams)
          $image.Dispose()
        }
      }
    }
    
    #Use function:
    # cd to directory with png files
    cd .\bin\pngTest
    
    
    #Run ConvertTo-Jpg function
    Get-ChildItem *.png | ConvertTo-Jpg
    

提交回复
热议问题