我和另一个家伙正在尝试共同编写一个批处理文件,该文件将使用ImageMagick命令执行各种图像操作。
我们让它工作,我决定将它移动到硬盘上的另一个位置。 这个剧本突然不再起作用了。
它会创建“已修改”文件夹,但不会执行图像转换。 我可以从命令提示符执行convert命令,但不能使用脚本。
我换了电脑,过了一会儿它再次发生了!
我不知道发生了什么。 我试过:
没有任何成功! 请帮助我提供任何有用的提示!
环境:
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
答案 0 :(得分: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
答案 1 :(得分: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