如何用convert.exe编写批处理代码批量转换文件夹及子文件夹PNG为JPG
Batch Convert All PNGs (Including Subfolders) to JPG with ImageMagick
Hey there! Let's turn that single-file ImageMagick command into a batch solution that handles every PNG in your target folder and all its subfolders. Here are a couple of reliable approaches depending on what shell you prefer:
1. Windows Batch Script (.bat)
This is perfect if you want a double-clickable script that works in traditional Command Prompt. It recursively scans for PNGs and converts each one to a JPG with a white background.
@echo off setlocal enabledelayedexpansion :: Set your target folder (use "." for current directory, or full path like "C:\MyImages") set "target_folder=." :: Recursively loop through all .png files for /r "%target_folder%" %%f in (*.png) do ( :: Generate the output JPG path (replace .png with .jpg) set "jpg_file=%%~dpnf.jpg" :: Run ImageMagick conversion with white background (flatten removes transparency) convert "%%f" -background white -flatten "!jpg_file!" echo Converted: "%%f" → "!jpg_file!" ) echo. echo All PNG conversions completed successfully! pause
Key Details:
@echo offhides raw command prompts for cleaner outputsetlocal enabledelayedexpansionensures variables work correctly inside the loopfor /rrecursively scans the target folder and all subfolders for.pngfiles%%~dpnfextracts the file's path and name (we swap.pngfor.jpgto get the output path)-flattenis critical: it merges transparent layers into the white background (without this, transparent areas might turn black)- The
echoline tracks progress so you can see which files are being converted
2. PowerShell Script (More Robust for Special Characters)
If you're comfortable with PowerShell, this script handles filenames with spaces, accents, or special characters more smoothly than traditional batch:
# Set your target folder path (use "." for current directory, or full path like "C:\MyImages") $targetFolder = "." # Get all PNG files recursively and convert each to JPG Get-ChildItem -Path $targetFolder -Filter *.png -Recurse | ForEach-Object { $jpgPath = [System.IO.Path]::ChangeExtension($_.FullName, ".jpg") & convert $_.FullName -background white -flatten $jpgPath Write-Host "Converted: $($_.FullName) → $jpgPath" } Write-Host "`nAll conversions finished!"
Key Details:
Get-ChildItem -Recursegrabs every PNG in the target folder and subfolders[System.IO.Path]::ChangeExtensionsafely replaces the file extension without manual string tweaks&runs the externalconvertcommand from ImageMagick- PowerShell natively handles tricky filenames that might break batch scripts
Important Notes:
- Make sure ImageMagick is installed and the
convertcommand is in your system PATH. If not, replaceconvertwith the full path to the executable (e.g.,"C:\Program Files\ImageMagick-7.1.1-Q16-HDRI\convert.exe") - The scripts preserve original PNG files. If you want to delete them after conversion, add
del "%%f"(batch) orRemove-Item $_.FullName(PowerShell) right after theconvertline (use this cautiously!) - If you don't need to flatten transparent layers, you can remove the
-flattenparameter, but keep-background whitefor consistent output
内容的提问来源于stack exchange,提问作者Mhwmd
相关产品推荐
相关产品推荐

