【问题标题】:watermark png image with its created date带有创建日期的水印 png 图像
【发布时间】:2022-01-06 01:50:50
【问题描述】:

我需要用创建日期为我的 png 图像添加水印。

我正在尝试读取png 文件exif data。但是在linux上使用screenshot工具捕获的png文件没有exif数据。

我正在使用以下脚本为我的 png 图像添加水印及其创建日期:

#!/bin/bash

echo "Script for addding time stamp"
date --iso-8601=seconds  
shopt -s extglob

find . -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.tif" -o \
 -iname "*.tiff" -o -iname "*.png" | 

## Go through the results, saving each as $img
while IFS= read -r img; do
    ## Find will return full paths, so an image in the current
    ## directory will be ./foo.jpg and the first dot screws up 
    ## bash's pattern matching. Use basename and dirname to extract
    ## the needed information.
    name=$(basename "$img")
    path=$(dirname "$img")
    ext="${name/#*./}";
    
    ## Check whether this file has exif data
    if exiv2 "$img" 2>&1 | grep timestamp >/dev/null 
    ## If it does, read it and add the water mark   
    then
    echo "Processing $img...";
    convert "$img" -gravity SouthEast  -pointsize 22 -fill black \
             -annotate +30+30  %[exif:DateTimeOriginal] \
             "$path"/"${name/%.*/.time.$ext}";
    ## If the image has no exif data, use the creation date of the file.
    else
    echo "No Exif data in $img...";
      date=$(stat "$img" | grep Modify | cut -d ' ' -f 2,3 | cut -d ':' -f1,2)
      convert "$img" -gravity SouthEast  -pointsize 22 -fill black \
             -annotate +30+30  "$date" \
             "$path"/"${name/%.*/.time.$ext}";
    fi 
done

预期的水印格式输出:

但我需要具有以下日期格式的水印(如其输出$ date --iso-8601=seconds)-

2021-11-29T07:46:15+01:00

实际水印格式输出: 但是png图片stat没有这种格式,所以我的水印是-

2021-11-29 07:27

谁能建议我如何修改我的脚本以在我的 png 图像上获得预期的水印。

还有其他最好的方法来为 png 图像添加水印及其创建日期。

【问题讨论】:

    标签: bash date png exif


    【解决方案1】:

    请在备用目录中的一些图像的副本上尝试此操作,因为我尚未对其进行测试。我认为exiftool 会将文件修改日期添加到您的图像中,而不是 EXIF DateTimeOriginal:

    exiftool -v "-FileModifyDate>DateTimeOriginal" *.png
    

    您可以像这样查看有关图像的所有时间相关数据的摘要:

    exiftool -time:all -G1 -a -s SOMEIMAGE.PNG
    [System]        FileModifyDate                  : 2021:11:23 09:48:30+00:00
    [System]        FileAccessDate                  : 2021:11:27 13:38:21+00:00
    [System]        FileInodeChangeDate             : 2021:11:26 23:41:21+00:00
    

    如果您将exiftool 标签添加到您的问题中,一位专家可能会建议您如何将其更改为仅添加尚未存在的数据。


    另外,如果您有很多图像,请考虑使用 GNU Parallel。只需在搜索框中输入[gnu-parallel]image

    【讨论】:

      【解决方案2】:

      如果您想调整图像文件上stat 命令的输出, 请你试试:

      datestr=$(date --iso-8601=seconds -d @$(stat --printf='%Z\n' "$img"))
      

      假设您的date 命令支持-d 选项。
      顺便说一句,我已将变量名称更改为 datestr 以避免命名空间 冲突,虽然它是无害的,只是令人困惑。

      【讨论】:

      • 也感谢您的回答。我也试过你的答案。它也运作良好。但我考虑过作为@dan 的答案发布的类似答案。
      • 好的。没关系。 (虽然我在几分钟内就打败了他 :)
      【解决方案3】:

      在 Linux 上,你可以像这样得到你想要的日期格式:

      date=$(date -d "@$(stat -c %Y "$img")" --iso-8601=seconds)
      
      • stat -c %Ystat 输出使用格式说明符
      • %Y 是最后修改的日期,以纪元秒为单位
      • GNU date -d @<epoch-seconds> 指定一个输入 日期,@ 指定纪元秒格式
      • 然后指定输出格式(--iso-8601=seconds
      • BSD/Mac 对stat 的语法略有不同

      【讨论】:

      • 它按照预期的输出工作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-01
      • 2014-06-29
      • 2016-10-06
      • 2011-02-16
      相关资源
      最近更新 更多