Watermarking images in multiple sub-directories

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?".
philipt18
Posts: 9
Joined: 2012-12-04T13:51:10-07:00
Authentication code: 6789

Watermarking images in multiple sub-directories

Post by philipt18 »

I have a directory that has dozens of sub-directories. Each subdirectory has multiple images. Some directories have jpegs and some have PDFs. I want to convert all the images so they are all jpegs, and they all have a common watermark. I can duplicate the main directory first, so I don't need to move files or recreate sub-directories. If I duplicate the directory first, what command can I use to make sure everything is a jpeg and watermarked?

If I convert PDFs, can the same command delete the PDFs after converting them to jpegs?

Also, as the files are not all the same size, is there a way to size the watermark as a percentage of the image size? i.e. scale the watermark image to be centered, 60% of the underlying image size, and 30% opacity.

Thanks,

Philip
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Watermarking images in multiple sub-directories

Post by Bonzo »

IM Version and platform?
philipt18
Posts: 9
Joined: 2012-12-04T13:51:10-07:00
Authentication code: 6789

Re: Watermarking images in multiple sub-directories

Post by philipt18 »

ImageMagick 6.6.0-4 running on Debian on a Dreamhost server.

In theory I could compile a newer version on the server, but I'd prefer to use the version already there if I can.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Watermarking images in multiple sub-directories

Post by fmw42 »

mogrify, which processes all the images in a given directory, will not allow a second watermark image. You will need to write a script to loop over your image in each directory and use convert to process them.

see
http://www.imagemagick.org/Usage/basics/#mogrify
http://www.imagemagick.org/Usage/basics/#mogrify_not

Placing a watermark needs to know the pixel offset. I do not believe that you can use percent directly. So you will need to compute the pixel offsets from the percent of the image size.

xoffset=`convert image -format "%[fx:percent*w/100]" info:

will convert to pixels from percent and put that into the variable xoffset. Then you need to use composite or convert to do the watermark using -gravity and -geometry for the offset. See http://www.imagemagick.org/Usage/compose/#watermark
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Watermarking images in multiple sub-directories

Post by anthony »

Little more complex than that. The offset needs to be a percentage of (main_image_size - watermark_image_size)

That way 0% is hard left (or top) and 100% is hard right (or bottom)

Alternativally you may like to specify the center of the watermark, EG: 0% has half the watermark of the left edge.
In whcih case it is (percentage of the main_image_size) - watermark_image_size/2
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
philipt18
Posts: 9
Joined: 2012-12-04T13:51:10-07:00
Authentication code: 6789

Re: Watermarking images in multiple sub-directories

Post by philipt18 »

Is there a way to get mogrify to act recursively and change all the images in the sub folders?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Watermarking images in multiple sub-directories

Post by fmw42 »

philipt18 wrote:Is there a way to get mogrify to act recursively and change all the images in the sub folders?
No, not that I am aware.

Again some other approach is needed, either a script or something from http://www.imagemagick.org/Usage/basics/#mogrify_not
philipt18
Posts: 9
Joined: 2012-12-04T13:51:10-07:00
Authentication code: 6789

Re: Watermarking images in multiple sub-directories

Post by philipt18 »

Could someone help me with a bash script that would go through all sub-directories and first convert all PDFs to JPGs, then watermark all JPGs with the same image? To make it easy the watermark image can just be one size and the gravity can be set to center so it will just put the image in the middle of all the images. I'll run this all on a duplicate directory structure, so all the images can be overwritten, which I think is easier. I think this just needs one convert line to convert the PDFs and one composite (or another convert) to do the watermarking, but my bash skills are limited and I could use the help to get it to traverse all the subdirectories and convert and watermark all the files. The only important note here is that not all the sub-directories have PDFs, in some JPGs already exist.

I really appreciate whatever help can be provided.

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

Re: Watermarking images in multiple sub-directories

Post by fmw42 »

you would probably be better off (easier) making a list of your directories and then running mogrify on each one separately. You can make a list of mogrify commands in a text file, one for each directory. The just cut and paste into a terminal window. Check mogrify with composite, you may be able to do it all in one command. Mogrify will let you specify to process only pdf files if you want from each directory. see

http://www.imagemagick.org/Usage/basics/#mogrify
http://www.imagemagick.org/Usage/basics ... fy_compose

Alternately, make a loop over a mogrify command that is fed a list of all your directories. You can get a list of recursive directories from

find <base directory> -type d

where <base directory> is the main directory that you want to start searching recursively for other subdirectories

So the looping would be something like the following (though untested):

cd
list=`find <base directory> -type d`
for directory in $list; do
cd $directory
mogrify -format .jpg *.pdf
cd
done

However, I would suggest you use -path in mogrify to write your results to a new directory. But that is up to you.

Note I have not tried watermarking with -draw to know if that works within mogrify --- see second link above

Can you provide a link to at least one input image and the image you want to use as a watermark and a resulting image, so we can see how you want them composited?
philipt18
Posts: 9
Joined: 2012-12-04T13:51:10-07:00
Authentication code: 6789

Re: Watermarking images in multiple sub-directories

Post by philipt18 »

I've put up files at http://glnk.org/watermark/

The file igra-logo-trans-500px.png is the watermark file. It's 500x500px and it has a transparent background. I'm not sure, but I'd probably want to reduce the opacity of the final watermark to not more than 50%, maybe even 30%. I could do this in advance, but it would be nice to be able to set that in the script if possible.

There's a sample sub-directory 1907_CZ with 3 jpegs inside.

Unfortunately there's no set size of the files to be watermarked. As I mentioned earlier I'd love a way to size the watermark based on a percentage of the overall image size (i.e. 50% of the width or something like that) but to keep things simple I can just use the 500px watermark centered in the image.

The full set has over 90 sub-directories, each one with between 1 and hundreds of images.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Watermarking images in multiple sub-directories

Post by fmw42 »

I have downloaded your images. I created a directory structure as follows

Code: Select all

phillipt18_igra_logo_trans_500px_0p5.png
phillipt18
	1907_CZ1.jpg
	1907_CZ2.jpg
	1907_CZ3.jpg
	phillipt18a
			1907_CZ1.jpg
			1907_CZ2.jpg
			1907_CZ3.jpg
		phillipt18a1
			1907_CZ1.jpg
			1907_CZ2.jpg
			1907_CZ3.jpg
		phillipt18a2
			1907_CZ1.jpg
			1907_CZ2.jpg
			1907_CZ3.jpg
		phillipt18b
		1907_CZ1.jpg
		1907_CZ2.jpg
		1907_CZ3.jpg
I then modified your watermark image as follows to make its transparency half of what it was:

Code: Select all

convert phillipt18_igra_logo_trans_500px.png -alpha on -channel a -evaluate multiply 0.5 +channel phillipt18_igra_logo_trans_500px_0p5.png
I then ran the following to make the watermark image resized to 250x250 and place it at 100 pixels up from the bottom center (south) of every jpg image.

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

It did give error messages for each subdirectory[/color], but still worked fine to traverse the directory and add the watermark image to each of the other images.


mogrify: Empty input file `phillipt18a' @ error/jpeg.c/JPEGErrorHandler/318.

I think the error is coming because mogrify cannot tell the difference between a subdirectory and a file. Thus it sees the subdirectory as an empty file, gives an error message and then continues on.

You can pre-adjust the transparency as desired by my convert command above to whatever you want. You can also replace south with center and change the 100 to 0 to get the watermark in the center. Use any other gravity setting to justify the watermark as desired.

see the image option at http://www.imagemagick.org/script/magic ... aphics.php for details of the compose method and syntax

see mogrify also at http://www.imagemagick.org/Usage/basics/#mogrify for syntax on mogrify
philipt18
Posts: 9
Joined: 2012-12-04T13:51:10-07:00
Authentication code: 6789

Re: Watermarking images in multiple sub-directories

Post by philipt18 »

thank you, I'll give it a shot.
philipt18
Posts: 9
Joined: 2012-12-04T13:51:10-07:00
Authentication code: 6789

Re: Watermarking images in multiple sub-directories

Post by philipt18 »

In mogrifying PDFs to JPGs I noticed a huge reduction in resolution, which is a big problem. I suspect it kept the size, but lowered the resolution from the scan resolution to 72dpi or something. Does mogrify default to something like that? How do I get it to keep the resolution when converting the PDFs into JPGs? Thanks.
philipt18
Posts: 9
Joined: 2012-12-04T13:51:10-07:00
Authentication code: 6789

Re: Watermarking images in multiple sub-directories

Post by philipt18 »

I see in other discussions this resolution loss problem when converting from PDF to JPG. It suggests using the -density setting to counteract that. Seems odd that there is no way to get it to keep the resolution as is. If you know of a way, I would love to hear it.

Also, I noticed your mogrify command uses -format jpg, but as it's operating on *.jpg, what does that do? Thanks.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Watermarking images in multiple sub-directories

Post by snibgo »

See, for example, Low resolution when converting small PDF to png.

As I understand it, there is no "as is" resolution for PDF files. They can be reproduced at any resolution. By default, IM doesn't supersample the pixels so it is fast, but supersampling is frequently needed for decent quality.
snibgo's IM pages: im.snibgo.com
Post Reply