I need to watermark my png images with its created date.
I am trying to read png
files exif data
. But Captured png
file with screenshot
tool on linux doesn't have exif data.
I am using below script to watermark my png images with its created date :
#!/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
Expected Watermark format output:
But I need watermark with below date format (like its output $ date --iso-8601=seconds
) -
2021-11-29T07:46:15 01:00
Actual WaterMark format output:
But png image stat
doesn't have this format so my watermark comes as -
2021-11-29 07:27
Can any one suggest me how can I modify my script to get the expected watermark on my png image.
OR
Is there any another best way to watermark png images with its date created.
CodePudding user response:
On Linux, you can get the date format you want like this:
date=$(date -d "@$(stat -c %Y "$img")" --iso-8601=seconds)
stat -c %Y
to use format specifiers forstat
output%Y
is the date of last modification, in epoch seconds- GNU
date -d @<epoch-seconds>
to specify an input date,@
specifies epoch seconds format - Then specify the output format (
--iso-8601=seconds
) - BSD/Mac has slightly different syntax for
stat
CodePudding user response:
Please try this on a COPY of a few images in a spare directory as I have not tested it. I think exiftool
will add file modification date to your images in place of EXIF DateTimeOriginal:
exiftool -v "-FileModifyDate>DateTimeOriginal" *.png
You can see a summary of all time-related data about an image like this:
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
If you add the exiftool
tag to your question, one of the specialists will probably suggest how you can change it to only add the data if not already present.
Also, consider using GNU Parallel if you have lots of images. Just put [gnu-parallel]
in the Search box, along with image
.
CodePudding user response:
If you want to tweak the output of the stat
command on the image file,
would you please try:
datestr=$(date --iso-8601=seconds -d @$(stat --printf='%Z\n' "$img"))
It assumes your date
command supports -d
option.
BTW I've changed the variable name to datestr
to avoid the namespace
conflict although it is harmless and just confusing.