Windows环境下借助ImageMagick批量递归调整图片至原尺寸50%
Got it, let's get this sorted for you. Since you already have ImageMagick and Git for Windows installed, we can combine Git Bash's shell tools with ImageMagick's powerful resizing commands to handle all 4110 images across those 250 nested folders. Here are two reliable methods:
Method 1: Use find Command (Most Robust)
This method will recursively scan every subfolder and target all common image formats. It’s great for large-scale batches because it handles nested directories seamlessly.
Open Git Bash and navigate to your root folder (replace the path with your actual root directory):
cd /c/Path/To/Your/Root/FolderRun the command below. Note: Use
magick convertfor newer ImageMagick versions (v7+); if you have an older version, drop themagickprefix and just useconvert:# For ImageMagick v7+ find . -type f \( -iname "*.jpg" -o -iname "*.png" -o -iname "*.gif" -o -iname "*.bmp" -o -iname "*.jpeg" \) -exec magick convert {} -resize 50% {} \; # For older ImageMagick versions find . -type f \( -iname "*.jpg" -o -iname "*.png" -o -iname "*.gif" -o -iname "*.bmp" -o -iname "*.jpeg" \) -exec convert {} -resize 50% {} \;
Breakdown of the command:
find .: Starts searching from the current directory (your root folder)-type f: Targets only files (ignores folders)\( -iname "*.jpg" -o ... \): Matches all common image formats, case-insensitive (so.JPGand.jpgboth get picked up)-exec ... \;: Runs the ImageMagick command on each matched file-resize 50%: Scales the image to 50% of its original dimensions{}: Represents the current file being processed (the first{}is the input, the second is the output—this overwrites the original file)
Method 2: Use Recursive for Loop (Git Bash Specific)
If you prefer a more readable loop structure, this works well in Git Bash:
- Open Git Bash and navigate to your root folder as before.
- Run these commands:
# Enable recursive globbing (lets **/ match all nested folders) shopt -s globstar # Loop through all image files for img in **/*.{jpg,jpeg,png,gif,bmp,JPG,JPEG,PNG,GIF,BMP}; do # Only process if the file actually exists (avoids empty matches) if [ -f "$img" ]; then magick convert "$img" -resize 50% "$img" # For older ImageMagick: convert "$img" -resize 50% "$img" fi done
Critical Precaution
Before running the full batch, always test with a small subset first to avoid accidental data loss:
- Replace the resizing part with a print command to verify which files will be processed. For example, in the
findcommand, change-exec ... \;to-print:find . -type f \( -iname "*.jpg" -o -iname "*.png" \) -print - If you don’t want to overwrite original images, you can modify the command to save resized copies to a new directory structure. Let me know if you need help with that!
Troubleshooting
- If ImageMagick commands aren’t recognized: Check if the ImageMagick installation folder is in your system PATH. You can also use the full path to the executable, e.g.,
"C:\Program Files\ImageMagick-7.1.1-Q16-HDRI\magick.exe"instead of justmagick.
内容的提问来源于stack exchange,提问作者hsantos

