在Windows上的ImageMagick缩略图批处理

时间:2022-06-16 09:01:24

I am using ImageMagick to create thumbnails of photos.

我正在使用ImageMagick来创建照片的缩略图。

I am using windows OS.

我正在使用windows操作系统。

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:

目标文件名与源文件名相同,但修改为1个字符:

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

注:从L到M只需要一个字母

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

源文件名模式总是:*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)

1)如何遍历每个文件夹和子文件夹,然后将其输出到目标驱动器上相同的结构中(如果文件夹不存在,就创建它们)

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

2)如何修改输出文件名,使*l*.jpeg变为*m*.jpeg。

3 个解决方案

#1


2  

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

1)这是你如何从C:\源代码到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.

这个for循环的作用是遍历一个文件夹树,从C:\source开始,每个文件夹的名称都在变量%I中。命令“md”是“mkdir”的缩写。修改器“~pnx”去掉了原始驱动器的字母,这样X:\dest可以被前置。注意,在“X:\dest”之后没有反斜杠,因为原始路径的第一个反斜杠被保留。

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

2)使用相同的命令运行Imagick命令来处理JPEG文件:

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

目标名称是相同的。3)通过Naru (http://hp.vector.co.jp/authors/VA014830/english/FlexRena/)使用flexbler珐琅工具重命名目标树中的所有jpeg。它可以使用正则表达式来选择和修改名称。你会重命名

"(\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.

意思:至少有一个小数,后面跟着一个字母“L”,后面至少还有一个字符。名字被第一部分,一个字面的“M”和第二部分所取代。

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

编辑:处理步骤2)需要一个文件选择器来工作,如下所示:

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.

否则,只处理文件夹名——根据“转换”程序,这可能足够,也可能不够。

#2


3  

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"

)

#3


2  

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"
)

#1


2  

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

1)这是你如何从C:\源代码到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.

这个for循环的作用是遍历一个文件夹树,从C:\source开始,每个文件夹的名称都在变量%I中。命令“md”是“mkdir”的缩写。修改器“~pnx”去掉了原始驱动器的字母,这样X:\dest可以被前置。注意,在“X:\dest”之后没有反斜杠,因为原始路径的第一个反斜杠被保留。

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

2)使用相同的命令运行Imagick命令来处理JPEG文件:

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

目标名称是相同的。3)通过Naru (http://hp.vector.co.jp/authors/VA014830/english/FlexRena/)使用flexbler珐琅工具重命名目标树中的所有jpeg。它可以使用正则表达式来选择和修改名称。你会重命名

"(\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.

意思:至少有一个小数,后面跟着一个字母“L”,后面至少还有一个字符。名字被第一部分,一个字面的“M”和第二部分所取代。

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

编辑:处理步骤2)需要一个文件选择器来工作,如下所示:

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.

否则,只处理文件夹名——根据“转换”程序,这可能足够,也可能不够。

#2


3  

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"

)

#3


2  

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"
)