You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用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 off hides raw command prompts for cleaner output
  • setlocal enabledelayedexpansion ensures variables work correctly inside the loop
  • for /r recursively scans the target folder and all subfolders for .png files
  • %%~dpnf extracts the file's path and name (we swap .png for .jpg to get the output path)
  • -flatten is critical: it merges transparent layers into the white background (without this, transparent areas might turn black)
  • The echo line 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 -Recurse grabs every PNG in the target folder and subfolders
  • [System.IO.Path]::ChangeExtension safely replaces the file extension without manual string tweaks
  • & runs the external convert command from ImageMagick
  • PowerShell natively handles tricky filenames that might break batch scripts

Important Notes:

  • Make sure ImageMagick is installed and the convert command is in your system PATH. If not, replace convert with 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) or Remove-Item $_.FullName (PowerShell) right after the convert line (use this cautiously!)
  • If you don't need to flatten transparent layers, you can remove the -flatten parameter, but keep -background white for consistent output

内容的提问来源于stack exchange,提问作者Mhwmd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:05:11