2016-09-09 29 views
0

我需要一個腳本來填充3x3網格中5個塊的所有可能組合。所以我想我可以使用ImageMagick來着色和輸入一個txt文件。文本文件每行包含一個數字,數字顯示哪個塊應該着色。 5個數字形成一個可能的組合。用於循環和行延續的Windows批處理文件

我使用for循環來讀取我的txt文件行。一個變量(稱爲a)遞增,當它達到5時,它重置爲0,另一個變量(稱爲n)增加,這將更改輸出文件的名稱。由於每次開始新組合時都需要重新加載原始圖像,所以我使用另一個變量(名稱)在原始圖像和每個組合的圖片之間切換。

這是我第一次寫一個批處理文件。所以我可能還沒有完全理解,由於我的腳本沒有輸出,所以行延續和變量是如何工作的。

setlocal ENABLEDELAYEDEXPANSION 
set a=0 
set n=0 
for /F %%G in (test.txt) do^
set /A a=a+1 &^
if !a!==5 set a=0 & set /A n=n+1 &^
if !a!==0 set name=original.png else set name=output_!n!.png^
if %%G == 1 magick convert -fill blue -draw "color 50, 50 floodfill" %name% output_!n!.png^
if %%G == 2 magick convert -fill blue -draw "color 150, 50 floodfill" %name% output_!n!.png^
if %%G == 3 magick convert -fill blue -draw "color 250, 50 floodfill" %name% output_!n!.png^
if %%G == 4 magick convert -fill blue -draw "color 50, 150 floodfill" %name% output_!n!.png^
if %%G == 5 magick convert -fill blue -draw "color 150, 150 floodfill" %name% output_!n!.png^
if %%G == 6 magick convert -fill blue -draw "color 250, 150 floodfill" %name% output_!n!.png^
if %%G == 7 magick convert -fill blue -draw "color 50, 250 floodfill" %name% output_!n!.png^
if %%G == 8 magick convert -fill blue -draw "color 150, 250 floodfill" %name% output_!n!.png^
if %%G == 9 magick convert -fill blue -draw "color 250, 250 floodfill" %name% output_!n!.png 
Pause 

回答

2

這裏是你的代碼(由新的第一線分開)重新格式化只,

@echo off 
setlocal ENABLEDELAYEDEXPANSION 
set a=0 
set n=0 
for /F %%G in (test.txt) do (
    set /A a=a+1 
    if !a!==5 (set a=0) 
    set /A n=n+1 
    if !a!==0 (set name=original.png) else (set name=output_!n!.png) 
    if %%G==1 (magick convert -fill blue -draw "color 50, 50 floodfill" !name! output_!n!.png) 
    if %%G==2 (magick convert -fill blue -draw "color 150, 50 floodfill" !name! output_!n!.png) 
    if %%G==3 (magick convert -fill blue -draw "color 250, 50 floodfill" !name! output_!n!.png) 
    if %%G==4 (magick convert -fill blue -draw "color 50, 150 floodfill" !name! output_!n!.png) 
    if %%G==5 (magick convert -fill blue -draw "color 150, 150 floodfill" !name! output_!n!.png) 
    if %%G==6 (magick convert -fill blue -draw "color 250, 150 floodfill" !name! output_!n!.png) 
    if %%G==7 (magick convert -fill blue -draw "color 50, 250 floodfill" !name! output_!n!.png) 
    if %%G==8 (magick convert -fill blue -draw "color 150, 250 floodfill" !name! output_!n!.png) 
    if %%G==9 (magick convert -fill blue -draw "color 250, 250 floodfill" !name! output_!n!.png) 
) 
Pause 

我已經更新了代碼來解決一個重大問題與你的名字的變量。

+0

還有一些其他的錯誤(邏輯),但由於你的真棒格式化修復它們很容易!它現在有效。 –

+0

修復了主要問題 – Compo