Watermark stamp entire folder jpg with filename

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?".
Post Reply
fredrikh
Posts: 3
Joined: 2017-02-16T07:06:12-07:00
Authentication code: 1151

Watermark stamp entire folder jpg with filename

Post by fredrikh »

Is there a way to stamp/watermark a folder of several JPG images - where the watermark/stamp is the files filename?

Searched but could not find solution

Im a photographer and need to generata gallerys for customers where they can select photos from filename stamped/watermarked on to the photo/jpg itself

Ex.

FOLDER:
IMG_0001.JPG
IMG_0002.JPG
IMG_0003.JPG
IMG_0004.JPG
IMG_0005.JPG
IMG_0006.JPG
etc...

Desired output would be IMG_0001_copy.JPG where the filename "0001" or "IMG_0001" is watermarked/stamped on to the photo/JPG file?

(Does not matter if the original files are written over or not)

Also - ofcourse I would like to do this folder-wise ie. 100-500 files as batch

Preferebly as Windows batch (CMD) or Powershell (Windows 10)

Thanks!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Watermark stamp entire folder jpg with filename

Post by snibgo »

First, work out a command that gives a result you like. For example, Windows BAT syntax:

Code: Select all

convert IMG_0001.JPG -gravity SouthWest -pointsize 20 -annotate +5+5 %%t -fill White -annotate +4+4 %%t x.png
This gives white text with slight black shadow, at bottom-left. Adjust "-pointsize" as you want.

When that does what you want, put it inside a "for" loop, like this:

Code: Select all

for %%F in (IMG_*.JPG) do %IM%convert %%F -gravity SouthWest -pointsize 20 -annotate +5+5 %%t -fill White -annotate +4+4 %%t outdir/%~nF_copy.jpg
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Watermark stamp entire folder jpg with filename

Post by GeeMack »

fredrikh wrote: 2017-02-16T07:16:00-07:00Is there a way to stamp/watermark a folder of several JPG images - where the watermark/stamp is the files filename? [...] Desired output would be IMG_0001_copy.JPG where the filename "0001" or "IMG_0001" is watermarked/stamped on to the photo/JPG file?
You can use "mogrify" to print filenames on all the images in an entire directory. A command like this would get it done...

Code: Select all

mogrify -pointsize 24 -strokewidth 4 -fill black -stroke black ^
   -gravity south -annotate +0+20 "%[f]" -fill white -stroke none -annotate +0+20 "%[f]" *.jpg
That would print the filename on every JPG, in a 24 point font, white with a black outline, centered horizontally and located 20 pixels up from the bottom. It will overwrite the existing files with their same original filenames.

If you're using ImageMagick 7 you'll need to put "magick " at the beginning of that command.

If your project requires changing the filenames, you can rename them in bulk, before or after you add the filename stamps, with a simple "for" loop something like this...

Code: Select all

for %F in ( *.jpg ) do ren "%F" "%~nF_copy%~xF"
That would insert "_copy" into every filename, like "IMG_0003.JPG" will be renamed to "IMG_0003_copy.JPG", etc. Of course if you change the filenames before you do the annotation, those new names will be what gets printed on the images.

If you need to use different pointsizes or locate the stamp at different places on each image depending on its dimensions or orientation, etc., there would be other ways to handle it. You could, for example, put the IM command into a "for" loop so it processes each file according to its own criteria as suggested by snibgo above.
fredrikh
Posts: 3
Joined: 2017-02-16T07:06:12-07:00
Authentication code: 1151

Re: Watermark stamp entire folder jpg with filename

Post by fredrikh »

Thanks!
First command line worked!

Second alternative generated this (running CMD as admin, current directory same as the one holding the images, convert.exe copied to same folder)

*TEST 1:

D:\STAMP_IN>for %F in (IMG_*.JPG) do %IM%convert %F -gravity SouthWest -pointsize 40 -annotate +5+5 %t -fill Red -annotate +4+4 %t outdir/%~nF_copy.jpg

D:\STAMP_IN>%IM%convert IMG_0188.jpg -gravity SouthWest -pointsize 40 -annotate +5+5 %t -fill Red -annotate +4+4 %t outdir/IMG_0188_copy.jpg
'%IM%convert'

is not recognized as an internal or external command,
operable program or batch file.

CHANGES:
( Changed the %% to % as Im running from command line for testing + changed some text formatting [40 font and red] + convert.exe copied to current working directory - dont wanna mess with looong paths :) )


*TEST 2

( Changed %IM%convert --> convert )

D:\STAMP_IN>for %F in (IMG_*.JPG) do convert %F -gravity SouthWest -pointsize 40 -annotate +5+5 %t -fill Red -annotate +4+4 %t outdir/%~nF_copy.jpg

D:\STAMP_IN>convert IMG_0188.jpg -gravity SouthWest -pointsize 40 -annotate +5+5 %t -fill Red -annotate +4+4 %t outdir/IMG_0188_copy.jpg
convert: unable to open image 'outdir/IMG_0188_copy.jpg': No such file or directory @ error/blob.c/OpenBlob/2691.

D:\STAMP_IN>convert IMG_0197.jpg -gravity SouthWest -pointsize 40 -annotate +5+5 %t -fill Red -annotate +4+4 %t outdir/IMG_0197_copy.jpg
convert: unable to open image 'outdir/IMG_0197_copy.jpg': No such file or directory @ error/blob.c/OpenBlob/2691.

D:\STAMP_IN>convert IMG_0198.jpg -gravity SouthWest -pointsize 40 -annotate +5+5 %t -fill Red -annotate +4+4 %t outdir/IMG_0198_copy.jpg
convert: unable to open image 'outdir/IMG_0198_copy.jpg': No such file or directory @ error/blob.c/OpenBlob/2691.

D:\STAMP_IN>convert IMG_0322.jpg -gravity SouthWest -pointsize 40 -annotate +5+5 %t -fill Red -annotate +4+4 %t outdir/IMG_0322_copy.jpg
convert: unable to open image 'outdir/IMG_0322_copy.jpg': No such file or directory @ error/blob.c/OpenBlob/2691.

D:\STAMP_IN>convert IMG_0323.jpg -gravity SouthWest -pointsize 40 -annotate +5+5 %t -fill Red -annotate +4+4 %t outdir/IMG_0323_copy.jpg
convert: unable to open image 'outdir/IMG_0323_copy.jpg': No such file or directory @ error/blob.c/OpenBlob/2691.

TEST 3:
( added "outdir" [D:\STAMP_IN\outdir\ -- current directory D:\STAMP_IN)

D:\STAMP_IN>for %F in (IMG_*.JPG) do convert %F -gravity SouthWest -pointsize 300 -annotate +5+5 %t -fill Red -annotate +4+4 %t outdir/%~nF_copy.jpg

SUCCESS! :)

(pointsize 300 because my test images were high res DSRL photos)

Thanks! Great! This is perfect for me!
Now I can just export 900 px images from Lightroom to special project folder - then run the .cmd batch file in same folder and a cup of coffee later I have them all watermarked with the unique image number stamped in. (And after that I export all images to my http server and send the client a link - and client sends me a list of numbers of the photos they want)

THANKS ALOT!







snibgo wrote: 2017-02-16T07:36:24-07:00 First, work out a command that gives a result you like. For example, Windows BAT syntax:

Code: Select all

convert IMG_0001.JPG -gravity SouthWest -pointsize 20 -annotate +5+5 %%t -fill White -annotate +4+4 %%t x.png
This gives white text with slight black shadow, at bottom-left. Adjust "-pointsize" as you want.

When that does what you want, put it inside a "for" loop, like this:

Code: Select all

for %%F in (IMG_*.JPG) do %IM%convert %%F -gravity SouthWest -pointsize 20 -annotate +5+5 %%t -fill White -annotate +4+4 %%t outdir/%~nF_copy.jpg
fredrikh
Posts: 3
Joined: 2017-02-16T07:06:12-07:00
Authentication code: 1151

Re: Watermark stamp entire folder jpg with filename

Post by fredrikh »

Thanks again! This worked perfect at first try! THANKS!

Im gonna use this solution :)

GeeMack wrote: 2017-02-16T08:34:40-07:00
fredrikh wrote: 2017-02-16T07:16:00-07:00Is there a way to stamp/watermark a folder of several JPG images - where the watermark/stamp is the files filename? [...] Desired output would be IMG_0001_copy.JPG where the filename "0001" or "IMG_0001" is watermarked/stamped on to the photo/JPG file?
You can use "mogrify" to print filenames on all the images in an entire directory. A command like this would get it done...

Code: Select all

mogrify -pointsize 24 -strokewidth 4 -fill black -stroke black ^
   -gravity south -annotate +0+20 "%[f]" -fill white -stroke none -annotate +0+20 "%[f]" *.jpg
That would print the filename on every JPG, in a 24 point font, white with a black outline, centered horizontally and located 20 pixels up from the bottom. It will overwrite the existing files with their same original filenames.

If you're using ImageMagick 7 you'll need to put "magick " at the beginning of that command.

If your project requires changing the filenames, you can rename them in bulk, before or after you add the filename stamps, with a simple "for" loop something like this...

Code: Select all

for %F in ( *.jpg ) do ren "%F" "%~nF_copy%~xF"
That would insert "_copy" into every filename, like "IMG_0003.JPG" will be renamed to "IMG_0003_copy.JPG", etc. Of course if you change the filenames before you do the annotation, those new names will be what gets printed on the images.

If you need to use different pointsizes or locate the stamp at different places on each image depending on its dimensions or orientation, etc., there would be other ways to handle it. You could, for example, put the IM command into a "for" loop so it processes each file according to its own criteria as suggested by snibgo above.
Post Reply