Page 1 of 2
Convert Watermark Bash Script to CLI Command
Posted: 2016-12-07T09:16:20-07:00
by buchert
How do I convert this watermark bash script to a CLI command? I don't need YAD (Yet Another Dialog) to work with it, so that can be taken out. And it doesn't need to work on multiple files.
Code: Select all
#!/bin/sh
command -v convert >/dev/null 2>&1 || { echo >&2 "I require convert, but it's not installed. aborting!";exit 1;}
command -v identify >/dev/null 2>&1 || { echo >&2 "I require identify, but it's not installed. aborting!";exit 1;}
command -v bc >/dev/null 2>&1 || { echo >&2 "I require bc, but it's not installed. aborting!";exit 1;}
basedir="$(dirname "$(readlink -f "${1}")")"
cd "$basedir"
caption=$(yad --entry --editable --no-buttons --width 410 \
--title="Please enter your caption and press enter:")
if [ -z "$caption" ]; then
printf "no caption was selected, aborting!\n"
exit 1
fi
printf "caption is $caption\n"
if [ ! -d "$basedir"/backups ]; then
mkdir -p "$basedir"/backups
fi
while [ $# -gt 0 ]; do
file="$1"
if [ -s "$file" ]; then
cp -f "$file" backups
export imagesize=$(identify -format "%w,%h" "$file")
export imagewidth=$(echo "$imagesize" | cut -f1 -d',')
export imageheight=$(echo "$(echo "$imagesize" | cut -f2 -d',')*0.06000" | bc)
convert -background '#000000' -fill white -gravity center \
-size $(echo $imagewidth)x$(echo $imageheight) caption:"$caption" \
"$file" +swap -gravity south -composite "$file" && \
printf "\n$file watermarked successfully\n"
fi
shift
done
Re: Convert Watermark Bash Script to CLI Command
Posted: 2016-12-07T10:54:09-07:00
by Bonzo
So basically you just want a piece of code to add a text watermark an image?
Have you tried a search on the forum?
Re: Convert Watermark Bash Script to CLI Command
Posted: 2016-12-07T12:16:50-07:00
by buchert
I want these exact settings though. I've used this bash script a lot and like it.
Re: Convert Watermark Bash Script to CLI Command
Posted: 2016-12-07T12:19:04-07:00
by buchert
This is the main code:
Code: Select all
convert -background '#000000' -fill white -gravity center \
-size $(echo $imagewidth)x$(echo $imageheight) caption:"$caption" \
"$file" +swap -gravity south -composite "$file"
But I need the command to find the dimensions of the input, so the watermark will be place at the bottom center.
Re: Convert Watermark Bash Script to CLI Command
Posted: 2016-12-07T12:32:30-07:00
by Bonzo
It will be simpler if you can update to a 7 version of Imagemagick - all on one line.
Re: Convert Watermark Bash Script to CLI Command
Posted: 2016-12-07T15:02:42-07:00
by fmw42
In IM 6, you will have to find the dimensions in a separate command. In IM 7 you can do it all in one command.
IM 6
Code: Select all
dim=$(convert "$file" -format "%wx%h" info:)
convert "$file" \
\( -background '#000000' -fill white -gravity center -size $dim caption:"$caption"\) \
-gravity south -composite "$file"
IM 7
Code: Select all
magick "$file" -set option:dim "%wx%h"\
\( -background '#000000' -fill white -gravity center -size "%[dim]" caption:"$caption"\) \
-gravity south -composite "$file"
You may also want to add -trim after the caption to remove extra white space
Re: Convert Watermark Bash Script to CLI Command
Posted: 2016-12-07T15:10:54-07:00
by Bonzo
I did not know you could do -set option:dim fmw42 I would have probably done something like -size "%wx%h" - untested
Re: Convert Watermark Bash Script to CLI Command
Posted: 2016-12-07T15:22:56-07:00
by snibgo
-size "%wx%h" would also probably work here. For example, this works:
Code: Select all
magick rose: -size %wx%h gradient: info:
Re: Convert Watermark Bash Script to CLI Command
Posted: 2016-12-07T15:56:36-07:00
by fmw42
I was not sure if it would get the size of the input image or the text image and I did not test. So I played it safe.
Re: Convert Watermark Bash Script to CLI Command
Posted: 2016-12-08T22:37:02-07:00
by anthony
I am glad to see the additions to make percent escapes more global starting to get good use!
Re: Convert Watermark Bash Script to CLI Command
Posted: 2016-12-09T10:17:04-07:00
by Bonzo
Yes it is very useful Anthony and cuts down the amount of code.
Re: Convert Watermark Bash Script to CLI Command
Posted: 2016-12-12T11:21:06-07:00
by buchert
I have ImageMagick 7.0.3-7 Q16 x64 installed:
magick -version
Version: ImageMagick 7.0.3-7 Q16 x64 2016-11-15
http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License:
http://www.imagemagick.org/script/license.php
Visual C++: 180040629
Features: Cipher DPC Modules OpenMP
Delegates (built-in): bzlib cairo flif freetype jng jp2 jpeg lcms lqr openexr pangocairo png ps rsvg tiff webp xml zlib
I tried this code:
Code: Select all
magick "$file" -set option:dim "%wx%h"\
\( -background '#000000' -fill white -gravity center -size "%[dim]" caption:"$caption"\) \
-gravity south -composite "$file"
and after executing the command just hangs and doesn't do anything.
When replacing both instances of "$file" with the actual name of the input file and desired output file with this command:
Code: Select all
magick butterfly.jpg -set option:dim "%wx%h"\
\( -background '#000000' -fill white -gravity center -size "%[dim]" caption:"$caption"\) \
-gravity south -composite butterflydone.jpg
it gives me these results:
Input image:
Output image:
I tried with -trim added to the command:
Code: Select all
magick butterfly.jpg -set option:dim "%wx%h"\
\( -background '#000000' -fill white -gravity center -size "%[dim]" caption:"$caption"\) -trim \
-gravity south -composite butterflydone.jpg
and it gives me this output:
Ideally I'd like the watermark to give me an output like this:
That was creating using the original watermarking bash script.
fmw42 wrote:
IM 7
Code: Select all
magick "$file" -set option:dim "%wx%h"\
\( -background '#000000' -fill white -gravity center -size "%[dim]" caption:"$caption"\) \
-gravity south -composite "$file"
You may also want to add -trim after the caption to remove extra white space
Re: Convert Watermark Bash Script to CLI Command
Posted: 2016-12-12T11:49:32-07:00
by fmw42
There must be spaces after \( and before \) and before ending \. You have not conformed.
magick "$file" -set option:dim "%wx%h"\
\( -background '#000000' -fill white -gravity center -size "%[dim]" caption:"$caption"\) \
-gravity south -composite "$file"
try
Code: Select all
magick "$file" -set option:dim "%wx%h" \
\( -background '#000000' -fill white -gravity center -size "%[dim]" caption:"$caption" \) \
-gravity south -composite "$file"
Re: Convert Watermark Bash Script to CLI Command
Posted: 2016-12-12T11:57:50-07:00
by fmw42
Try this to get what you want in your last example.
Code: Select all
magick butterfly.jpg \
\( -background '#000000' -fill white -gravity center -size "x30" label:"Testing" \) \
-gravity south -background black -append butterfly_wm.jpg
or
Code: Select all
magick butterfly.jpg -set option:dim "%w" \
\( -background '#000000' -fill white -gravity center -size "%[dim]x30" label:"Testing" \) \
-gravity south -composite butterfly_wm.jpg
Re: Convert Watermark Bash Script to CLI Command
Posted: 2016-12-13T02:12:38-07:00
by buchert
Thanks, but this doesn't work. The command hangs and doesn't do anything.
fmw42 wrote:There must be spaces after \( and before \) and before ending \. You have not conformed.
try
Code: Select all
magick "$file" -set option:dim "%wx%h" \
\( -background '#000000' -fill white -gravity center -size "%[dim]" caption:"$caption" \) \
-gravity south -composite "$file"