Convert all images to jpg

后端 未结 2 1664
深忆病人
深忆病人 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:55

    I've done something similar with converting a bitmap files to icon files here:

    http://sev17.com/2011/07/creating-icons-files/

    I adapted it to your requirements and test a function for converting image files to jpg:

    function ConvertTo-Jpg
    {
        [cmdletbinding()]
        param([Parameter(Mandatory=$true, ValueFromPipeline = $true)] $Path)
    
        process{
            if ($Path -is [string])
            { $Path = get-childitem $Path }
    
            $Path | foreach {
                $image = [System.Drawing.Image]::FromFile($($_.FullName));
                $FilePath = [IO.Path]::ChangeExtension($_.FullName, '.jpg');
                $image.Save($FilePath, [System.Drawing.Imaging.ImageFormat]::Jpeg);
                $image.Dispose();
            }
        }
    
     }
    
     #Use function:
     #Cd to directory w/ png files
     cd .\bin\pngTest
    
     #Run ConvertTo-Jpg function
     Get-ChildItem *.png | ConvertTo-Jpg
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题