Convert Watermark Bash Script to CLI Command

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
buchert
Posts: 36
Joined: 2015-02-13T11:15:29-07:00
Authentication code: 6789

Convert Watermark Bash Script to CLI Command

Post 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
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Convert Watermark Bash Script to CLI Command

Post 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?
buchert
Posts: 36
Joined: 2015-02-13T11:15:29-07:00
Authentication code: 6789

Re: Convert Watermark Bash Script to CLI Command

Post by buchert »

I want these exact settings though. I've used this bash script a lot and like it.
buchert
Posts: 36
Joined: 2015-02-13T11:15:29-07:00
Authentication code: 6789

Re: Convert Watermark Bash Script to CLI Command

Post 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.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Convert Watermark Bash Script to CLI Command

Post by Bonzo »

It will be simpler if you can update to a 7 version of Imagemagick - all on one line.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert Watermark Bash Script to CLI Command

Post 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
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Convert Watermark Bash Script to CLI Command

Post by Bonzo »

I did not know you could do -set option:dim fmw42 I would have probably done something like -size "%wx%h" - untested
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert Watermark Bash Script to CLI Command

Post by snibgo »

-size "%wx%h" would also probably work here. For example, this works:

Code: Select all

magick rose: -size %wx%h gradient: info:
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert Watermark Bash Script to CLI Command

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Convert Watermark Bash Script to CLI Command

Post by anthony »

I am glad to see the additions to make percent escapes more global starting to get good use!
:-)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Convert Watermark Bash Script to CLI Command

Post by Bonzo »

Yes it is very useful Anthony and cuts down the amount of code.
buchert
Posts: 36
Joined: 2015-02-13T11:15:29-07:00
Authentication code: 6789

Re: Convert Watermark Bash Script to CLI Command

Post 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:

Image

Output image:

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:

Image

Ideally I'd like the watermark to give me an output like this:

Image

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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert Watermark Bash Script to CLI Command

Post 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"
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert Watermark Bash Script to CLI Command

Post 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
buchert
Posts: 36
Joined: 2015-02-13T11:15:29-07:00
Authentication code: 6789

Re: Convert Watermark Bash Script to CLI Command

Post 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"
Post Reply