【问题标题】:How to modify this script to add date instead of date and time to the picture?如何修改此脚本以在图片中添加日期而不是日期和时间?
【发布时间】:2017-09-15 08:23:07
【问题描述】:

我遇到了这个有用的程序来为照片添加日期和时间。如何修改 bat 文件,使其只添加日期而不添加日期时间?

例如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/

【问题讨论】:

    标签: batch-file imagemagick


    【解决方案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”只读取“identify”命令的输出直到第一个空格,这只是日期部分。然后它将您的“%timestamp%”变量设置为该值并像以前一样继续。

    【讨论】:

      【解决方案2】:

      我不会说那种可怕的 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
      

      【讨论】:

      • 不幸的是,这不起作用,因为 EXIF 标签包含一个包含日期和时间的字符串,并且没有其他标签只有日期。所以解决方案是解析/剪切输出,留给读者作为练习:)
      • @xenoid 我知道FOR /F TOKENS=... 正是这样做的——它将行拆分为单独的项目以分配给连续的变量,即在这种情况下为%k....ss64.com/nt/for_cmd.html
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-25
      • 2023-02-15
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      相关资源
      最近更新 更多