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

如何用ImageMagick的Mogrify批量缩放JPG并保留源目录结构?

Fixing Directory Structure Preservation for Batch Image Resizing with ImageMagick

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 the for loop correctly—critical because batch processes loop variables differently than regular variables.
  • for /r %%f in (*.jpg): Iterates over every JPG file in source_dir and 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 is J:\Photos_test\Vacation\Beach\sunset.jpg, %%~pf gives \Vacation\Beach\).
  • mkdir "%target_dir%!rel_path!" 2>nul: Creates the matching subdirectory in the target folder. The 2>nul suppresses error messages if the directory already exists (no need to worry about duplicates).
  • magick convert: We use convert instead of mogrify here because it gives us explicit control over the output path—mogrify is designed to overwrite source files by default, which we don't want here. The %%~nf keeps the original filename without the extension, so your resized files have the same name as the source.

How to Use It

  1. Copy the script into a text file and save it with a .bat extension (e.g., ResizePhotos.bat).
  2. Adjust the source_dir, target_dir, and resize_settings variables to match your needs.
  3. Double-click the .bat file 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:09:07