如何在ImageMagick中将文件按文件名的部分匹配与数字范围合并?

huangapple go评论61阅读模式
英文:

How to merge files with partial matches of filename to numeric range in ImageMagick

问题

以下是翻译好的内容:

有一系列编号的文件:

out0001.tif, out0002.tif, ..., out0100.tif, out0101.tif, ..., out2300.tif

我想将它们合并成分层的tif文件(使用ImageMagick convert),每个文件应包含100个层。例如:

merged_1-100.tif, merged_100-200.tif, ...

当我运行以下命令时,它尝试将所有tif文件组合成一个庞大的文件,但我只需要前100个:

convert out00??.tif out0100.tif -adjoin merged_1-100.tif

我需要在命令中进行哪些修改?

英文:

There are a list of numbered files:

out0001.tif, out0002.tif, ..., out0100.tif, out0101.tif, ..., out2300.tif

And I'd like to merged them into layered tif (using ImageMagick convert) that should contain 100 layers. E. g.

merged_1-100.tif, merged_100-200.tif, ...

When I run the following cmd, it try to group all tifs in one huge, but I need only first hundred:

convert out00??.tif out0100.tif -adjoin merged_1-100.tif

What I need to modify in the command?

答案1

得分: 0

I found a solution using a batch file.

@echo off

set file_list=
set /a start_index=1
set /a finish_index=100

for /l %%G in (%start_index%,1,%finish_index%) do call :make-filelist %%G

rem Check
echo %file_list%

pause

rem Make conversion
convert %file_list% -adjoin merged_%start_index%-%finish_index%.tif

goto :eof

:make-filelist
    rem Add leading zeros
    set "fn=0000%1"
    set "fn=%fn:~-4%"
    
    rem Add filename to list of files
    set "file_list=%file_list%out%fn%.tif "
    goto :eof

It allows using any range of file indexes. To do so it is necessary to change start_index and finish_index.

英文:

I found a solution using a batch file.

@echo off

set file_list=
set /a start_index=1
set /a finish_index=100

for /l %%G in (%start_index%,1,%finish_index%) do call :make-filelist %%G

rem Check
echo %file_list%

pause

rem Make conversion
convert %file_list% -adjoin merged_%start_index%-%finish_index%.tif

goto :eof


:make-filelist
    rem Add leading zeros
	set "fn=0000%1"
	set "fn=%fn:~-4%"
	
	rem Add filename to list of files
    set "file_list=%file_list%out%fn%.tif "
	goto :eof

It allows using any range of file indexes. To do so it is necessary to change start_index and finish_index.

答案2

得分: 0

这个解决方案处理磁盘上的所有文件,并自动为输出文件生成正确的(有序的)名称。

@echo off
setlocal

set "hundred=100"

:nextHundred
if not exist out%hundred:~1%01.tif goto :EOF

echo Merging hundred %hundred:~1%
set /A "next=hundred+1"
convert out%hundred:~1%*.tif[0-99] out%next:~1%00.tif -adjoin merged_%hundred:~1%01-%next:~1%00

set "hundred=%next%"
goto nextHundred

我没有测试这个解决方案,因为我没有convert应用程序和数据文件。这个解决方案依赖于提问者发布的“convert out*.tif[0-99]将按字母顺序获取前100个文件”的评论。

英文:

This solution process all files existent on disk and automatically generates the correct (ordered) names for the output files.

@echo off
setlocal

set "hundred=100"

:nextHundred
if not exist out%hundred:~1%01.tif goto :EOF

echo Merging hundred %hundred:~1%
set /A "next=hundred+1"
convert out%hundred:~1%*.tif[0-99] out%next:~1%00.tif -adjoin merged_%hundred:~1%01-%next:~1%00

set "hundred=%next%"
goto nextHundred

I have not tested this solution because I have not convert application nor data files. This solution relies on the "convert out*.tif[0-99] will get the first 100 files in alphabetic order" comment posted by the OP.

huangapple
  • 本文由 发表于 2023年2月18日 01:44:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487617.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定