2

我和另一个家伙正在尝试共同编写一个批处理文件,该文件将使用 ImageMagick 命令执行各种图像操作。

我们让它工作,我决定把它移到硬盘上的另一个位置。突然之间,脚本不再工作了。

它会创建“已修改”文件夹,但不执行图像转换。我可以从命令提示符执行转换命令,但不能使用脚本。

我换了电脑,过了一会儿又出现了!

我不知道发生了什么。我试图:

  • 将其移回原始位置
  • 按照 IT 人群中 Roy 的建议重新启动计算机
  • 重新安装 ImageMagick
  • 向 IT 上帝祈祷(他可能有很多名字:Gates、Jobs、Thorvald、null 等)

一点成功都没有!请帮助我提供任何有用的提示!

环境:

视窗 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
4

2 回答 2

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
于 2013-04-29T06:54:31.633 回答
0

A folder dropped on the batfile never end with a slash so you can delete that part.

Try this:

@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
于 2012-11-25T16:23:52.870 回答