George Filippakos
George Filippakos

Reputation: 16569

ImageMagick thumbnail Batch processing on Windows

I am using ImageMagick to create thumbnails of photos.

I am using windows OS.

My source files are contained in numerous sub folders.

I wish to make thumbnails of the source files by saving to a destination folder on a different drive while preserving the same folder structure and modifying the original filename.

The destination file name is the same as the source but with 1 character modified:

Source examples:

c:\images\1\1L0000021.jpg
c:\images\1\1L000561.jpg
c:\images\2\234L0000032.jpg
c:\images\3\31214L000001.jpg

To dest drive:

d:\images\1\1M0000021.jpg
d:\images\1\1M000561.jpg
d:\images\2\234M0000032.jpg
d:\images\3\31214M000001.jpg

Note: only one letter need be changed from L to M

The source filename pattern is always: *l*.jpg

Here is the command I want to run to do the image processing:

convert -thumbnail 200x220^^ -gravity center -extent 200x200 -quality 80 c:\images\*.jpeg d:\images\output.jpeg

The above command creates the desired thumbnails from the source folder and saves into the destination folder but the output filename is incorrect and it does not traverse sub folders.

Questions:

1) how to traverse each folder and subfolder and then output to the exact same structure on the destination drive (create the folders if they don't exist)

2) how to modify the output filename so that *l*.jpeg becomes *m*.jpeg

Upvotes: 3

Views: 2157

Answers (3)

George Filippakos
George Filippakos

Reputation: 16569

UPDATE

Here is my final code in case it helps anybody in the future:

::You must first install ImageMagick

::This job converts large photos into 200x200 thumbnails and saves the new files to the specified destination.

@echo off 
setlocal enabledelayedexpansion

SET "@SOURCE=C:\Test\photos\"
SET "@DEST=C:\dest"

REM Replicate source folder structure to destination:

for /r %@SOURCE% %%D in (.) do (

    echo Creating folder: %@DEST%%%~pnxD
    @md "%@DEST%%%~pnxD"

)


REM Select images from source using wildcard:

for /r %@SOURCE% %%F in (*l*.jpeg) do (

    REM Modify file name (i'm replacing 'l' for 'm')  

    set "fname=%%~nF"
    set "fname=!fname:l=m!"


    REM Convert source image to thumbnail and save to destination:

    REM echo Source: %%F
    echo Saving: %@DEST%%%~pnxF

    @convert -thumbnail 200x220^^^^ -gravity center -extent 200x200 -quality 80 "%%F" "%@DEST%%%~pF!fname!%%~xF"

)

Upvotes: 3

user1016274
user1016274

Reputation: 4209

1) this is how you duplicate a folder tree from C:\source to X:\dest:

for /r C:\source %I in (.) do @md "X:\dest%~pnxI"

What this for loop does is traverse a folder tree, starting at C:\source, with each folder name in the variable %I. The command "md" is short for "mkdir". The modifiers "~pnx" strip off the original drive letter, so that X:\dest can be prepended. NOTE that there is no backslash after "X:\dest" as the first backslash of the original path is preserved.

2) Use the same command to run the Imagick command to process your JPEG files:

for /r C:\source %I in (.) do @convert -options %I "X:\dest%~pnxI"

The target name is identical. 3) Rename all JPEGs in the destination tree using the FlexibleRenamer tool by Naru (http://hp.vector.co.jp/authors/VA014830/english/FlexRena/). It can use a Regular Expression to select and modify names. You would rename

"(\d+)L(.+)"

to

"\1M\2"

meaning: at least one decimal, followed by an literal "L", followed by at least one more character. The name is replaced by the first part, a literal "M" and the second part.

edit: The processing step 2) needs a file selector to work, like this:

for /r C:\source %I in (*.jpg) do @convert -options %I "X:\dest%~pnxI"

Otherwise, only folder names are processed - which may or may not suffice depending on the 'convert' program.

Upvotes: 2

Dany Bee
Dany Bee

Reputation: 552

Try this:

@echo off &setlocal enabledelayedexpansion
for /r "c:\images" %%a in (*.jpg) do (
    set "fname=%%~na"
    set "fname=!fname:L=M!"
    md "D:%%~pa" 2>nul
    convert -thumbnail 200x220^^ -gravity center -extent 200x200 -quality 80 "%%~fa" "D:%%~da!fname!%%~xa"
)

Upvotes: 2

Related Questions