我需要使用其創建日期為我的 png 影像添加水印。
我正在嘗試讀取png檔案exif data。但是在 linux 上png使用screenshot工具捕獲的檔案沒有 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
實際stat水印格式輸出:
但 png 影像沒有這種格式,所以我的水印是 -
2021-11-29 07:27
任何人都可以建議我如何修改我的腳本以在我的 png 影像上獲得預期的水印。
或者
有沒有其他最好的方法來為 png 影像加上創建日期的水印。
uj5u.com熱心網友回復:
在 Linux 上,您可以獲得您想要的日期格式,如下所示:
date=$(date -d "@$(stat -c %Y "$img")" --iso-8601=seconds)
stat -c %Y使用格式說明符進行stat輸出%Y是最后修改的日期,以紀元秒為單位- GNU
date -d @<epoch-seconds>指定輸入日期,@指定紀元秒格式 - 然后指定輸出格式 (
--iso-8601=seconds) - BSD/Mac 的語法略有不同
stat
uj5u.com熱心網友回復:
請在備用目錄中的一些影像的 COPY 上嘗試此操作,因為我尚未對其進行測驗。我認為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。
uj5u.com熱心網友回復:
如果您想stat在影像檔案上調整命令的輸出,請嘗試:
datestr=$(date --iso-8601=seconds -d @$(stat --printf='%Z\n' "$img"))
它假定您的date命令支持-d選項。
順便說一句,我已將變數名稱更改為datestr以避免名稱空間沖突,盡管它是無害的并且只是令人困惑。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/369239.html
下一篇:使用bash搜索模式時忽略換行符
