如何修改此脚本以向图片添加日期而不是日期和时间?

时间:2017-09-15 08:23:07

标签: batch-file imagemagick

我遇到过这个有用的程序,可以为照片添加日期和时间。如何修改bat文件以便它只添加日期而不是日期时间?

e.g。 2017:12:10而不是2017:12:10 10:10:02

@echo off & cls
rem enable variables referencing themselves inside loops
SetLocal EnableDelayedExpansion

rem optional settings
set fontcolor=#FFD800
set fontoutlinecolor=#000000
set fontstyle="Arial-Bold"

rem create a new folder where the stamped images will be placed
mkdir stamped

rem loop through all jpg png jpeg and gif files in the current folder
for /f "delims=" %%a in ('dir /b /A:-D /T:C "%cd%\*.jpg" "%cd%\*.png" "%cd%\*.jpeg" "%cd%\*.gif"') do (
    rem retrieve image date and time
    SetLocal EnableDelayedExpansion
    for /f "tokens=1-2" %%i in ('identify.exe -ping -format "%%w %%h" "%cd%\%%a"') do set W=%%i& set H=%%j

    rem retrieve image timestamp to perform size and distance calculations on
    SetLocal EnableDelayedExpansion
    for /f "tokens=1-2 delims=" %%k in ('identify -format "%%[EXIF:DateTimeOriginal]" "%cd%\%%a"') do set timestamp=%%k

    rem set timestamp to no timestamp if there is no timestamp
    if "!timestamp!" == "" (
        set timestamp=No timestamp
    )

    rem print some information about the process
    echo %%a is !W! x !H! stamp !timestamp! ...

    rem set timestamp size to a fourth of the screen width
    set /A timestampsize = !W! / 3

    rem set timestamp offset distance from side of the screen
    set /A timestampoffset = !W! / 20

    rem set timestamp outline relative size
    set /A outlinewidth = !W! / 600

    rem echo !timestampsize! !timestampoffset!

    rem create a custom image with the timestamp with transparent background and combine it with the image
    convert.exe ^
    -verbose ^
    -background none^
    -stroke !fontoutlinecolor! ^
    -strokewidth !outlinewidth! ^
    -font !fontstyle! ^
    -fill !fontcolor! ^
    -size !timestampsize!x ^
    -gravity center label:"!timestamp!" "%cd%\%%a" +swap ^
    -gravity southeast ^
    -geometry +!timestampoffset!+!timestampoffset! ^
    -stroke !fontoutlinecolor! ^
    -strokewidth !outlinewidth! ^
    -composite "%cd%\stamped\%%a"

    endlocal
    endlocal
    echo.
)
endlocal
echo Complete!
pause

原始文件位置: https://nirklars.wordpress.com/2015/04/25/add-scaled-timestamps-to-photos/

2 个答案:

答案 0 :(得分:1)

您可以通过编辑此行来获取EXIF时间戳中的日期......

for /f "tokens=1-2 delims=" %%k in ('identify -format "%%[EXIF:DateTimeOriginal]" "%cd%\%%a"') do set timestamp=%%k

将该行更改为此...

for /f "tokens=1-2 delims= " %%k in ('identify -format "%%[EXIF:DateTimeOriginal]" "%cd%\%%a"') do set timestamp=%%k

这样你就可以使用空格作为分隔符" delims ="而不是" delims ="。然后你的变量"%k"只读取"识别"的输出。命令到第一个空格,这只是日期部分。然后它设置你的"%时间戳%"变量到该值并继续像以前一样。

答案 1 :(得分:0)

我不会说那种糟糕的Windows BATCH语言怪物,但我怀疑你需要更改这一行:

for /f "tokens=1-2 delims=" %%k in ('identify -format "%%[EXIF:DateTimeOriginal]" "%cd%\%%a"') do set timestamp=%%k

只接受第一个令牌,而不是前两个:

for /f "tokens=1 delims=" %%k in ('identify -format "%%[EXIF:DateTimeOriginal]" "%cd%\%%a"') do set timestamp=%%k

如果这不起作用,并且任何不理解 ImageMagick 但确实理解Windows BATCH的人,请执行以下命令:

identify -format "%%[EXIF:DateTimeOriginal]" someImage.jpg

产生这个:

2013:03:09 08:59:50