2012-11-25 157 views
2

我和另一個傢伙正試圖共同編寫一個批處理文件,該文件將使用ImageMagick命令執行各種圖像操作。ImageMagick批處理文件停止工作

我們得到它的工作,我決定將它移動到硬盤上的另一個位置。 突然之間腳本不再工作了。

它創建文件夾「修改」,但不執行圖像轉換。 我可以從命令提示符執行轉換命令,但不能使用腳本。

我換了電腦,過了一段時間它又發生了!

我不知道發生了什麼。 我曾嘗試:

  • 將其移回原來的位置
  • 重新啓動計算機,由羅伊勸在IT人羣
  • 重新安裝ImageMagick的
  • 祈求IT神(誰可能有許多名字:蓋茨,工作,Thorvald,空等)

沒有任何成就! 請幫助我的任何有用的提示!

環境:

64位Windows 7

的ImageMagick 6.8.0-6 Q16

的批處理文件內容如下:

@echo off 
    :: Drag and drop a folder of images on the BAT-file. 
    :: A 

    Setlocal enabledelayedexpansion 

    :: Removes the last slash if given in argument %1 
    Set "Dir=%~1" 
    IF "%DIR:~-1%" EQU "\" (Set "Dir=%DIR:~0,-1%") 

    :: Create the output folder if don't exist 
     MKDIR ".\modified" 2>NUL 

    :: Set maximium image height 
    SET /A "newHeight=780" 

    :: Set portrate extent width 
    SET /A "portrateWidth=585" 

    :: Read all the png and jpg images from the directory 
    FOR %%f IN ("%dir%\*.tif" "%dir%\*.jpg") DO (

     :: Set the variable width to the image width 
     For /F %%# in ('identify -ping -format "%%[fx:w]" "%%f"') Do (SET /A "width=%%#") 

     :: Set the variable height to the image height 
     For /F %%# in ('identify -ping -format "%%[fx:h]" "%%f"') Do (SET /A "height=%%#") 

     :: Check if the photo is portrate or landscape and run the relavant code 
     IF !height! LSS !width! (
      convert "%%f" -trim -resize x!newHeight! "modified\%%~nf.jpg" 
     ) ELSE (

    :: Only resize if height is over 780 
    IF !height! LSS !newHeight! (

      :: Calculation for portrate extent width 
      SET /A "newWidth=!height! * 3/4" 
      convert "%%f" -trim -resize x!height! -background blue -gravity center -extent !newWidth!x!height! "modified\%%~nf.jpg" 
) ELSE (
    convert "%%f" -trim -resize x!newHeight! -background blue -gravity center -extent !portrateWidth!x!newHeight! "modified\%%~nf.jpg" 
    ) 
      ) 
     ) 

PAUSE&EXIT 

回答

0

的文件夾下降在batfile上永遠不會以斜線結尾,因此您可以刪除該部分。

試試這個:如果用括號括起來

@echo off 
Setlocal enabledelayedexpansion 

:: First of all sets the imagemagick directory! 
PUSHD "C:\IMAGEMAGICK DIRECTORY" 

:: Drag and drop a folder of images on the BAT-file. 
:: A 

REM :: Removes the last slash if given in argument %1 
REM Set "Dir=%~1" 
REM IF "%DIR:~-1%" EQU "\" Set "Dir=%DIR:~0,-1%" 

:: Create the output folder if don't exist 
MKDIR "%~dp0modified" 2>NUL 

:: Set maximium image height 
SET /A "newHeight=780" 

:: Set portrate extent width 
SET /A "portrateWidth=585" 

:: Read all the png and jpg images from the directory 
FOR %%f IN ("%~1\*.tif" "%~1\*.jpg") DO (

    :: Set the variable width to the image width 
    For /F %%# in ('identify -ping -format "%%[fx:w]" "%%f"') Do (SET /A "width=%%#") 

    :: Set the variable height to the image height 
    For /F %%# in ('identify -ping -format "%%[fx:h]" "%%f"') Do (SET /A "height=%%#") 

    :: Check if the photo is portrate or landscape and run the relavant code 
    IF !height! LSS !width! (
     convert "%%f" -trim -resize x!newHeight! "%~dp0modified\%%~nf.jpg" 
    ) ELSE (

    :: Only resize if height is over 780 
     IF !height! LSS !newHeight! (
      :: Calculation for portrate extent width 
      SET /A "newWidth=!height! * 3/4" 
      convert "%%f" -trim -resize x!height! -background blue -gravity center -extent !newWidth!x!height! "%~dp0modified\%%~nf.jpg" 
     ) ELSE (
      convert "%%f" -trim -resize x!newHeight! -background blue -gravity center -extent !portrateWidth!x!newHeight! "%~dp0modified\%%~nf.jpg" 
     ) 
    ) 
) 

PAUSE&EXIT 
+0

感謝您的信息,但它並沒有解決我的問題。 完全相同的BAT文件正在我擁有的第三臺計算機上工作,並且也在我的共同編寫者計算機上工作。它沒有任何明顯的理由已停止在兩臺計算機上工作。 – MrDark

+0

如果相同的腳本在其他PC上工作,那麼問題似乎是imagemacgik,請確保您在PATH中具有所需的imagemagick路徑,在cmd中鍵入PATH並搜索imagemagick目錄。哦,如果你想放棄這個問題,使用imagemagick目錄裏面的腳本。 – ElektroStudios

+0

我已經更新了我的答案的代碼,請看第一條命令。 – ElektroStudios

0

set命令將無法正常工作。也改爲REM而不是雙引號的評論。

@echo off 
Setlocal enabledelayedexpansion 

REM Drag and drop a folder of images on the BAT-file. 
REM A 

REM Set directory to that of dragged file 
Set Dir=%~dp1 

REM Create the output folder if don't exist 
MKDIR "%~dp0modified" 2>NUL 

REM Set maximium image height 
SET /A "newHeight=780" 

REM Set portrate extent width 
SET /A "portrateWidth=585" 

REM Read all the png and jpg images from the directory 
FOR %%f IN ("%Dir%*.tif" "%Dir%*.jpg") DO (

REM Set the variable width to the image width 
For /F %%# in ('identify -ping -format "%%[fx:w]" "%%f"') Do (SET /A "width=%%#") 

REM Set the variable height to the image height 
For /F %%# in ('identify -ping -format "%%[fx:h]" "%%f"') Do (SET /A "height=%%#") 

REM Check if the photo is portrate or landscape and run the relavant code 
IF !height! LSS !width! (
    convert "%%f" -trim -resize x!newHeight! "%~dp0modified\%%~nf.jpg" 
) ELSE (

REM Only resize if height is over 780 
    IF !height! LSS !newHeight! (
     REM Calculation for portrate extent width 
     SET /A "newWidth=!height! * 3/4" 
     convert "%%f" -trim -resize x!height! -background blue -gravity center -extent !newWidth!x!height! "%~dp0modified\%%~nf.jpg" 
    ) ELSE (
     convert "%%f" -trim -resize x!newHeight! -background blue -gravity center -extent !portrateWidth!x!newHeight! "%~dp0modified\%%~nf.jpg" 
     ) 
    ) 
) 

PAUSE&EXIT 
+0

你的腳本的主要問題是沒有使用正確的工作目錄。請注意,您還可以使用cd/d%Dir%或chdir/d%Dir%來消除變量%Dir%中的反斜槓。您設置腳本的方式,如果您將單個文件拖入其中,則應找到並處理該文件所在的其他tif和jpg文件。 – pathe3

+0

您的腳本之前可能工作過,因爲它位於與圖像文件相同的目錄中。 – pathe3