如何用ImageMagick的Mogrify批量缩放JPG并保留源目录结构?
Hey there! I see you're trying to batch-resize JPGs while keeping your original directory structure, but your current command dumps all resized images into a single root folder. Let's fix that—here's a robust solution that'll mirror your source directory structure in the target folder.
The Problem with Your Original Command
Your existing command uses for /r /d to iterate over directories, then runs magick mogrify with -path. The issue? -path only specifies a single root target directory; it doesn't automatically create subdirectories matching your source structure. That's why all resized files end up in J:\Resized_test directly.
The Solution: Iterate Over Files, Create Directories First
Instead of looping through directories, we'll loop through each individual JPG file. For each file, we'll extract its relative path from the source directory, create that same path in the target folder, then resize and save the file there.
Here's the complete batch script:
@echo off setlocal enabledelayedexpansion rem Configure your paths and resize settings here set "source_dir=J:\Photos_test" set "target_dir=J:\Resized_test" set "resize_settings=1920x1080^>" cd /d "%source_dir%" rem Loop through every JPG file in source directory and subdirectories for /r %%f in (*.jpg) do ( rem Get the relative path of the file from the source directory set "rel_path=%%~pf" rem Create the matching directory in the target folder (ignore "already exists" errors) mkdir "%target_dir%!rel_path!" 2>nul rem Resize the image and save it to the corresponding target directory magick convert "%%f" -resize %resize_settings% "%target_dir%!rel_path!%%~nf.jpg" ) endlocal echo Success! All images resized, and directory structure mirrored in %target_dir%.
Breakdown of Key Parts
setlocal enabledelayedexpansion: This lets us use variables (!rel_path!) inside theforloop correctly—critical because batch processes loop variables differently than regular variables.for /r %%f in (*.jpg): Iterates over every JPG file insource_dirand all its subdirectories, so we handle every image individually.%%~pf: Extracts the directory path of the current file relative to the source directory (e.g., if the file isJ:\Photos_test\Vacation\Beach\sunset.jpg,%%~pfgives\Vacation\Beach\).mkdir "%target_dir%!rel_path!" 2>nul: Creates the matching subdirectory in the target folder. The2>nulsuppresses error messages if the directory already exists (no need to worry about duplicates).magick convert: We useconvertinstead ofmogrifyhere because it gives us explicit control over the output path—mogrifyis designed to overwrite source files by default, which we don't want here. The%%~nfkeeps the original filename without the extension, so your resized files have the same name as the source.
How to Use It
- Copy the script into a text file and save it with a
.batextension (e.g.,ResizePhotos.bat). - Adjust the
source_dir,target_dir, andresize_settingsvariables to match your needs. - Double-click the
.batfile to run it.
This will resize all your JPGs, create every subdirectory from your source in the target folder, and place each resized image in the matching subdirectory—exactly what you're looking for!
内容的提问来源于stack exchange,提问作者JamesA

