The script converts all PNG files in the specified folder tree to JPEG format running four workers in parallel
The attached powershell script searches all PNG files in the specified folder (including its subfolders) and converts them to JPEG, omitting PNG files with alpha channel used. If there is already a conversion from a previous run, the file is skipped. The filenames of the converted files get the extension for the target format appended.
For a conversion to JPEG format the following steps and their commands are used.
Determine if the PNG contains an alpha channel:
$out = C:\Tools\ImageMagick-7.1.0-portable-Q16-HDRI-x64\identify -format '%[channels]' "$path"
if ($out -eq "rgba" -or $out -eq "srgba") {In case it contains one, the script checks, if it is used or not (opaque):
$out = C:\Tools\ImageMagick-7.1.0-portable-Q16-HDRI-x64\convert "$path" -format "%[opaque]" info:The PNG files without an alpha channel are first converted to a 24-bit PNG:
C:\Tools\ImageMagick-7.1.0-portable-Q16-HDRI-x64\convert $path $path_new24and then via bisection method converted to JPEG files with a compression value between 70 and 100:
$result = C:\Tools\_mozjpeg-v4.0.3-win-x64\cjpeg.exe -quality $quality -outfile "$fn_jpg" "$path" 2>&1until the visual difference between the original PNG and the converted JPEG is below a defined threshold of 1%:
$output = C:\Tools\_ssim-master\rmgr-ssim.exe -y "$path" "$fn_jpg"
...To avoid having to repeat the steps for PNG files with an alpha channel, the files are tagged in the Alternate Data Stream (ADS) of the file. This allows the file to be easily identified and skipped during the next run.
You need to adjust the paths in the script to your installation folders of ImageMagick, Mozilla JPEG Encoder and ssim.
In lines 110-111 of the file convert_png_to_jpg.ps1 you can specify folders to exclude and a pattern for filenames to exclude (e.g. filename.skip.png).
The script makes use of an example implementation of producer / consumer parallelism in PowerShell by Lee Holmes.