Watermark all images in sub folders

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?".
New
Posts: 7
Joined: 2013-05-15T13:31:26-07:00
Authentication code: 6789

Watermark all images in sub folders

Post by New »

Hello

i have many sub folders, first part over 200 folders. I would like to watermark them.

I know to watermark one by one with this command:

composite -gravity center castle.gif frame.gif castle_button.gif

from http://www.imagemagick.org/Usage/annotating/#overlay

I would like to use script which will watermark all photos in sub folders of directory. I use Lubuntu.

I found that there is solution

viewtopic.php?f=1&t=22379#p93454

but i don't understand how to use it.

Could someone be so kind and help me with this problem.

Thank you.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Watermark all images in sub folders

Post by fmw42 »

Code: Select all

cd
list=`find /Users/fred/phillipt18 -type d`
for directory in $list; do
cd $directory
mogrify -format jpg -matte \
-draw 'gravity south image src-over 0,100 250,250 "/Users/fred/phillipt18_igra_logo_trans_500px_0p5.png"' *.jpg 
cd
done
cd -- changes directory to your home folder
list=`find /Users/fred/phillipt18 -type d` -- finds all the subdirectories in the folder at /Users/fred/phillipt18 and make a list of them
for directory in $list; do -- starts looping over each directory in the list
cd $directory -- changes directories to the given directory in the list
mogrify -format jpg -matte \
-draw 'gravity south image src-over 0,100 250,250 "/Users/fred/phillipt18_igra_logo_trans_500px_0p5.png"' *.jpg -- uses mogrify to overlay the watermark image (/Users/fred/phillipt18_igra_logo_trans_500px_0p5.png) to each image in the listed directory (it assumes the watermark image has some transparency so that is why the -matte is used)
cd -- change directories back to the users home directory
done -- iterate to next directory or end the loop

See
http://www.imagemagick.org/Usage/basics/#mogrify
http://www.imagemagick.org/Usage/basics ... fy_compose
http://www.imagemagick.org/script/magic ... aphics.php (for -draw "image...")
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Watermark all images in sub folders

Post by anthony »

of special note is non-IM scripting methods...

Mogrify-Not -- Batch Processing Alternatives
http://www.imagemagick.org/Usage/basics/#mogrify_not

And for Windows DOS scripting -- Batch processing a (sub-)directory tree
http://www.imagemagick.org/Usage/windows/#for_recursive

WARNING: make a backup of all originals before you even start!!!!
Even if the script creates the watermarked image in a separate file (or directory)
With this type of thing goes wrong, it generally goes wrong in a big way!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
New
Posts: 7
Joined: 2013-05-15T13:31:26-07:00
Authentication code: 6789

Re: Watermark all images in sub folders

Post by New »

Thank you guys for reply.
fmw42 wrote:

Code: Select all

cd
list=`find /Users/fred/phillipt18 -type d`
for directory in $list; do
cd $directory
mogrify -format jpg -matte \
-draw 'gravity south image src-over 0,100 250,250 "/Users/fred/phillipt18_igra_logo_trans_500px_0p5.png"' *.jpg 
cd
done
cd -- changes directory to your home folder
list=`find /Users/fred/phillipt18 -type d` -- finds all the subdirectories in the folder at /Users/fred/phillipt18 and make a list of them
for directory in $list; do -- starts looping over each directory in the list
cd $directory -- changes directories to the given directory in the list
mogrify -format jpg -matte \
-draw 'gravity south image src-over 0,100 250,250 "/Users/fred/phillipt18_igra_logo_trans_500px_0p5.png"' *.jpg -- uses mogrify to overlay the watermark image (/Users/fred/phillipt18_igra_logo_trans_500px_0p5.png) to each image in the listed directory (it assumes the watermark image has some transparency so that is why the -matte is used)
cd -- change directories back to the users home directory
done -- iterate to next directory or end the loop

See
http://www.imagemagick.org/Usage/basics/#mogrify
http://www.imagemagick.org/Usage/basics ... fy_compose
http://www.imagemagick.org/script/magic ... aphics.php (for -draw "image...")


When i want to process i get this error

mogrify: unable to open image `*.jpg': @ error/blob.c/OpenBlob/2587.

I would be appreciate for any help.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Watermark all images in sub folders

Post by fmw42 »

what are you exact commands?
New
Posts: 7
Joined: 2013-05-15T13:31:26-07:00
Authentication code: 6789

Re: Watermark all images in sub folders

Post by New »

I will now explain in steps what i did.

1.I open gedit and past script.

2. Edit it

cd
list=`find /home/username/Desktop/folder1/folder2 -type d`
for directory in $list; do
cd $directory
mogrify -format jpg \
-draw 'gravity south image src-over 0,100 250,250 "/home/username/Desktop/folder1/logoname.png"' *.jpg
cd
done

3. Save as script.sh

4. I give it permission chmod 775

5. Then i just type in terminal

/home/ovo/Desktop/script.sh

Please let me know if there is something incorrect.

Thank you.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Watermark all images in sub folders

Post by fmw42 »

A script file needs to know what shell to use, typically it should be

#!/bin/bash
cd
list=`find /home/username/Desktop/folder1/folder2 -type d`
for directory in $list; do
cd $directory
mogrify -format jpg \
-draw 'gravity south image src-over 0,100 250,250 "/home/username/Desktop/folder1/logoname.png"' *.jpg
cd
done

And then it would be called by

bash scriptname.sh

But you can just paste the following into your terminal

cd
list=`find /home/username/Desktop/folder1/folder2 -type d`
for directory in $list; do
cd $directory
mogrify -format jpg \
-draw 'gravity south image src-over 0,100 250,250 "/home/username/Desktop/folder1/logoname.png"' *.jpg
cd
done

/home/username/Desktop/folder1/folder2
Is username actually your directory? or Do you really have a specific name in this path for username?
New
Posts: 7
Joined: 2013-05-15T13:31:26-07:00
Authentication code: 6789

Re: Watermark all images in sub folders

Post by New »

fmw42 wrote:A script file needs to know what shell to use, typically it should be

#!/bin/bash
cd
list=`find /home/username/Desktop/folder1/folder2 -type d`
for directory in $list; do
cd $directory
mogrify -format jpg \
-draw 'gravity south image src-over 0,100 250,250 "/home/username/Desktop/folder1/logoname.png"' *.jpg
cd
done

And then it would be called by

bash scriptname.sh

But you can just paste the following into your terminal

cd
list=`find /home/username/Desktop/folder1/folder2 -type d`
for directory in $list; do
cd $directory
mogrify -format jpg \
-draw 'gravity south image src-over 0,100 250,250 "/home/username/Desktop/folder1/logoname.png"' *.jpg
cd
done

/home/username/Desktop/folder1/folder2
Is username actually your directory? or Do you really have a specific name in this path for username?

Still not working. I tried direct paste in terminal and also i tried run trough script and i get same error.

I am not sure if is possible with this script but when i instead mogrify put convert

cd
list=`find /home/username/Desktop/folder1/folder2 -type d`
for directory in $list; do
cd $directory
convert -format jpg \
-draw 'gravity south image src-over 0,100 250,250 "/home/username/Desktop/folder1/logoname.png"' *.jpg
cd
done

i get this error

convert: missing an image filename `*.jpg' @ error/convert.c/ConvertImageCommand/3011.


My directory username is different, i just write as example.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Watermark all images in sub folders

Post by glennrp »

f you want to use "convert" rather than "mogrify", then instead of

Code: Select all

convert ... *.jpg
use

Code: Select all

for file in *.jpg
do
convert ... $file
done
New
Posts: 7
Joined: 2013-05-15T13:31:26-07:00
Authentication code: 6789

Re: Watermark all images in sub folders

Post by New »

glennrp wrote:f you want to use "convert" rather than "mogrify", then instead of

Code: Select all

convert ... *.jpg
use

Code: Select all

for file in *.jpg
do
convert ... $file
done
Thank you for reply but i am not sure if i putted this part correct.

Code: Select all

cd
list=`find /home/ovo/Desktop/Nesto/102204004 -type d`
for directory in $list; do
cd $directory
for file in *.jpg
do
convert
-draw 'gravity south image src-over 0,100 250,250 "/home/ovo/Desktop/Nesto/2.png"' *.jpg 
done


I prefer mogrify instead of convert, but as i am already in situation that if i don't find solution, i will have to do manual 200+ folders which is only first part, any working solution would make me happy.

Please help if someone have any idea.

Thank you.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Watermark all images in sub folders

Post by fmw42 »

you have two loops so you need two finishing "done" statements. Also you must not break an IM command line without a line continuation (\ for unix and ^ for windows). You also need to cd back to the home directory before continuing the directory loop

cd
list=`find /home/ovo/Desktop/Nesto/102204004 -type d`
for directory in $list; do
cd $directory
for file in *.jpg; do
convert -draw 'gravity south image src-over 0,100 250,250 "/home/ovo/Desktop/Nesto/2.png"' *.jpg
done
cd
done
New
Posts: 7
Joined: 2013-05-15T13:31:26-07:00
Authentication code: 6789

Re: Watermark all images in sub folders

Post by New »

Thank you for reply.

I still get error

convert: missing an image filename `*.jpg' @ error/convert.c/ConvertImageCommand/3011.


Do you have ideas or alternative solutions for watermarking all images in sub folderds?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Watermark all images in sub folders

Post by fmw42 »

Sorry I missed something in your command. The input images must come first. You were using mogrify syntax with convert and that will not work. I have fixed that error and improved it to skip directories when looking for files (as long as the file has period in it). The following works fine for me. I am putting the rose mage in the center of a bunch of logo images in several directories:

Code: Select all

cd
list=`find /Users/fred/images/test -type d`
for directory in $list; do
echo "directory=$directory"
cd $directory
imglist=`ls | grep "\."`
for file in $imglist; do
echo "file=$file"
filename=`convert $file -format "%t" info:`
convert $file -draw "gravity center image src-over 0,0 70,46 '/Users/fred/images/test/rose.png'" ${filename}_new.jpg
done
cd
done
Note the syntax in the line (and I swapped the order of your single and double quotes)

convert $file -draw "gravity center image src-over 0,0 70,46 '/Users/fred/images/test/rose.png'" ${filename}_new.jpg

Results

Code: Select all

directory=/Users/fred/images/test
file=rose.png
file=rose_new.jpg
file=rose_new_new.jpg
file=rose_new_new_new.jpg
directory=/Users/fred/images/test/test1
file=logo.jpg
file=logo1.jpg
file=logo2.jpg
directory=/Users/fred/images/test/test1/test3
file=logo.jpg
file=logo1.jpg
file=logo2.jpg
directory=/Users/fred/images/test/test2
file=logo.jpg
file=logo1.jpg
file=logo2.jpg
New
Posts: 7
Joined: 2013-05-15T13:31:26-07:00
Authentication code: 6789

Re: Watermark all images in sub folders

Post by New »

Thank you, its working now.

Could you tell me how can i use mogrify instead of convert?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Watermark all images in sub folders

Post by fmw42 »

I did that earlier, but you said it did not work for you. The following works for me. But it will replace all the images in your directory tree with ones with the watermark. So it is best if you make a backup of each directory in case of problems.


cd
list=`find /Users/fred/images/test -type d`
echo "list=$list"
for directory in $list; do
echo "directory=$directory"
cd $directory
mogrify -format jpg -draw "gravity center image src-over 0,0 70,46 '/Users/fred/images/test/rose.png'" *.jpg
cd
done


I think you would be better off staying with the convert version.
Post Reply