Batch file ImageMagick批处理文件已停止工作
我和另一个家伙正试图共同编写一个批处理文件,该文件将使用ImageMagick命令执行各种图像操作 我们让它工作,我决定把它移到硬盘上的另一个位置。 突然之间,剧本就不起作用了 它创建文件夹“修改”,但不执行图像转换。 我可以从命令提示符执行convert命令,但不能使用脚本 我换了电脑,过了一会儿又发生了 我不知道发生了什么事。 我曾尝试:Batch file ImageMagick批处理文件已停止工作,batch-file,imagemagick,Batch File,Imagemagick,我和另一个家伙正试图共同编写一个批处理文件,该文件将使用ImageMagick命令执行各种图像操作 我们让它工作,我决定把它移到硬盘上的另一个位置。 突然之间,剧本就不起作用了 它创建文件夹“修改”,但不执行图像转换。 我可以从命令提示符执行convert命令,但不能使用脚本 我换了电脑,过了一会儿又发生了 我不知道发生了什么事。 我曾尝试: 将其移回原始位置 按照Roy在IT群组中的建议,重新启动计算机 重新安装ImageMagick 向IT上帝祈祷(他可能有很多名字:盖茨、乔布斯、托瓦尔德
- 将其移回原始位置
- 按照Roy在IT群组中的建议,重新启动计算机
- 重新安装ImageMagick
- 向IT上帝祈祷(他可能有很多名字:盖茨、乔布斯、托瓦尔德等)
@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
Windows 7 64位
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
放置在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
如果用括号括起来,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
谢谢你的信息,但它并没有解决我的问题。同样的BAT文件也在我拥有的第三台计算机上工作,也在我的合著者计算机上工作。毫无疑问,它已在两台计算机上停止工作。如果相同的脚本在其他计算机上工作,则问题似乎是imagemacgik的问题,请确保您的路径中有所需的imagemagick路径,在cmd中键入path并搜索imagemagick dir。哦,如果你想解决这个问题,请使用imagemagick目录中的脚本。我已经更新了答案的代码,请看第一个命令。是的,imagemagick目录在路径中。它仍然不起作用。PUSHD的添加也没有帮助。但是当我把球棒移动到IM dir时,它却能工作!非常感谢!脚本的主要问题是没有使用正确的工作目录。请注意,还可以使用cd/d%Dir%或chdir/d%Dir%消除变量%Dir%中的反斜杠。按照设置脚本的方式,如果将单个文件拖动到脚本中,它将查找并处理该文件所在的其他tif和jpg文件。您的脚本可能以前工作过,因为它与图像文件位于同一目录中。