2015-10-03 26 views
0

以下snipet工作正常,並在/ sliced子文件夾中創建切片。imagemagic在Windows批量編號

FOR %%a in (C:\someFolder\*.png) DO (
    ECHO Processing file: "%%~nxa"  
    convert %%a -crop [email protected] +repage +adjoin %~dp0\sliced\%%~na.png 
)  

然而,所得到的瓷磚的編號是醜陋的。它的形式如下:FileName-0.png,FileName-1.png,

我想使它從1開始並使用下劃線,如:FileName_1.png,FileName_2.png, 我該如何才能去做?

回答

1

我不是在一個位置,以檢驗這一直到後來,但我認爲你可以現場數設置爲1這樣的:

convert image.jpg -crop ... -scene 1 ... 

,並應使編號從1開始,而不是0

另外,如果你把%d在你的輸出文件名,它會把場景號碼在那裏,所以你可能想

convert image.jpg -crop ... -scene 1 ... file_%d.jpg 

您可能需要加倍在Windows可怕的批處理語言百分號,或可能會在前面插入一個插入符號^。我認爲這是你需要插入符號...

這裏是砍了9×9像素的灰度圖像成片的例子:

convert xc:gray[9x9] -crop [email protected] -scene 23 +repage file_%d.png 

生成的文件

-rw-r--r-- 1 mark staff 263 4 Oct 11:18 file_23.png 
-rw-r--r-- 1 mark staff 263 4 Oct 11:18 file_24.png 
-rw-r--r-- 1 mark staff 263 4 Oct 11:18 file_25.png 
-rw-r--r-- 1 mark staff 263 4 Oct 11:18 file_26.png 
-rw-r--r-- 1 mark staff 263 4 Oct 11:18 file_27.png 
-rw-r--r-- 1 mark staff 263 4 Oct 11:18 file_28.png 
-rw-r--r-- 1 mark staff 263 4 Oct 11:18 file_29.png 
-rw-r--r-- 1 mark staff 263 4 Oct 11:18 file_30.png 
-rw-r--r-- 1 mark staff 263 4 Oct 11:18 file_31.png 

如果您希望場景編號爲零填充,例如使用%04d

convert xc:gray[9x9] -crop [email protected] -scene 23 +repage file_%04d.png 

生成的文件

-rw-r--r-- 1 mark staff 263 4 Oct 11:21 file_0023.png 
-rw-r--r-- 1 mark staff 263 4 Oct 11:21 file_0024.png 
-rw-r--r-- 1 mark staff 263 4 Oct 11:21 file_0025.png 
-rw-r--r-- 1 mark staff 263 4 Oct 11:21 file_0026.png 
-rw-r--r-- 1 mark staff 263 4 Oct 11:21 file_0027.png 
-rw-r--r-- 1 mark staff 263 4 Oct 11:21 file_0028.png 
-rw-r--r-- 1 mark staff 263 4 Oct 11:21 file_0029.png 
-rw-r--r-- 1 mark staff 263 4 Oct 11:21 file_0030.png 
-rw-r--r-- 1 mark staff 263 4 Oct 11:21 file_0031.png 

事實上,你也許可以避開窗戶的全醜陋for loop,延遲擴展,雙百分,感嘆號標誌亂用mogrify這樣的:

mogrify -crop [email protected] -scene 34 -path sliced *png 

其中,如果您在當前目錄中具有名爲a.pngb.png的文件將導致:

sliced/a-34.png 
sliced/a-35.png 
sliced/a-36.png 
sliced/a-37.png 
sliced/a-38.png 
sliced/a-39.png 
sliced/a-40.png 
sliced/a-41.png 
sliced/a-42.png 
sliced/b-34.png 
sliced/b-35.png 
sliced/b-36.png 
sliced/b-37.png 
sliced/b-38.png 
sliced/b-39.png 
sliced/b-40.png 
sliced/b-41.png 
sliced/b-42.png 

雖然我不認爲你可以用mogrify來實現下劃線和零填充。但你仍然可以使用convert,就像我在回答開始時演示的那樣。

在Windows中,您可能需要在百分號上加倍,或者用插入符號逃脫^ - 不確定那裏是否有瘋狂的語法。

+0

在批處理文件'%%'中需要傳遞一個'%'到命令行,是的。 – wOxxOm

+0

完美。基於這個答案,我想出了: '@ECHO OFF setlocal enabledelayedexpansion for %% a in(C:\ localhost \ peopletest \ branches \ dev \ design \ Matrices \ *。png)do(\t \t \t \t回波處理的文件: 「%%〜NXA」 轉換%%一個3×3 -crop @ + repage -scene 1 +鄰接%〜dp0sliced \ %%〜な_ %% 02d.png ) PAUSE' – SunWuKung

0
setlocal enabledelayedexpansion 
set count=1 
for %%a in (C:\someFolder\*.png) do (
    echo processing file: !count!: "%%~nxa"  
    convert %%a -crop [email protected] +repage +adjoin %~dp0sliced\%%~na_!count!.png 
    set /a count+=1 
) 

帶前導零,實際上是3個位數,如果你需要4位數字改變!lz:~-3!!lz:~-4!

setlocal enabledelayedexpansion 
set count=1 
for %%a in (C:\someFolder\*.png) do (
    set lz=0000!count! 
    echo processing file: !lz:~-3!: "%%~nxa"  
    convert %%a -crop [email protected] +repage +adjoin %~dp0sliced\%%~na_!lz:~-3!.png 
    set /a count+=1 
)