Page 2 of 3

Re: Watermark all images in sub folders

Posted: 2017-02-24T03:29:54-07:00
by gogy
Hi guys,

I see this post is quite old but it helped me out to set a script for mogrify.

Only problem I have is that it works only for subfolders without space or special characters.

Can you correct my script to work also with other characters. This would be like put "directory name" in double quotes or something. I am not familiar with UNIX.

Thank you

Code: Select all

#!/bin/bash
cd
list=`find /home/path/public_html/watermark/test/original -type d`
echo "list=$list"
for directory in $list; do
echo "directory=$directory"
cd $directory
mogrify -draw "gravity center image src-over 0,0 150,200 '/home/path/public_html/watermark/wm/wm.png'" *.jpg
cd
done

Re: Watermark all images in sub folders

Posted: 2017-02-24T11:12:55-07:00
by fmw42
What kind of characters? I believe they need to be UTF8 compatible.

------------

Please, always provide your IM version and platform when asking questions, since syntax may differ. Also provide your exact command line and if possible your images.

See the top-most post in this forum "IMPORTANT: Please Read This FIRST Before Posting" at viewtopic.php?f=1&t=9620

For novices, see

viewtopic.php?f=1&t=9620
http://www.imagemagick.org/script/comma ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/

Re: Watermark all images in sub folders

Posted: 2017-02-24T18:05:02-07:00
by gogy
I tried with space first. For example folder name "TOP 5". This will not be done because it shows error that folder TOP was not found.

Re: Watermark all images in sub folders

Posted: 2017-02-24T20:29:30-07:00
by fmw42
The spaces can be dealt with by changing the IFS to a \n. Is it the folder names with spaces or the file names with spaces or both?

I am more concerned about what characters are a problem? Are they typed from a UTF8 compatible editor? If not, then that could be the issue. You should test with one image using convert. If you still have trouble with the file name with foreign characters be sure they were created with UTF8 character codes.

You still have not told us your IM version and platform?

Re: Watermark all images in sub folders

Posted: 2017-02-24T20:42:43-07:00
by fmw42
With regard to spaces, try this

Code: Select all

#!/bin/bash
OLDIFS=$IFS
IFS=$'\n'
cd
list=`find /home/path/public_html/watermark/test/original -type d`
echo "list=$list"
for directory in $list; do
echo "directory=$directory"
cd $directory
mogrify -draw "gravity center image src-over 0,0 150,200 '/home/path/public_html/watermark/wm/wm.png'" *.jpg
cd
done
IFS=$OLDIFS

Re: Watermark all images in sub folders

Posted: 2017-02-26T16:54:52-07:00
by anthony
Why not make this a little more direct,
That is by using xargs, and feel it the file list as binary (-print0 option of "find").

Code: Select all

find /home/path/public_html/watermark/test/original -type f -name '*.jpg' -print0 |
  xargs -0r mogrify -draw "gravity center image src-over 0,0 150,200 '/home/path/public_html/watermark/wm/wm.png' "
This will would work just fine with filenames not only containing spaces but newlines, or other weirdness.

Not only that but adding a -P 5 will let you run 5 mogrify's simultaneously over the files (and give your machine a good workout :-)

If you want to watch the progress there are other options for xargs like -n 1 (one file per process) and -t (trace)
You can even have it ask you interactively if you want to process this file or skip it.

See Xargs
http://en.wikipedia.org/wiki/Xargs

Also this Discussion
viewtopic.php?t=17400&p=65283#p65283

Other solutions are listed in
http://www.imagemagick.org/Usage/basics/#mogrify_not

Re: Watermark all images in sub folders

Posted: 2017-03-02T07:26:53-07:00
by gogy
fmw42 wrote: 2017-02-24T20:42:43-07:00 With regard to spaces, try this

Code: Select all

#!/bin/bash
OLDIFS=$IFS
IFS=$'\n'
cd
list=`find /home/path/public_html/watermark/test/original -type d`
echo "list=$list"
for directory in $list; do
echo "directory=$directory"
cd $directory
mogrify -draw "gravity center image src-over 0,0 150,200 '/home/path/public_html/watermark/wm/wm.png'" *.jpg
cd
done
IFS=$OLDIFS
Hi guys,
sorry for late reply.
This script has done it!

Thank you both! For your help.

Best regards

Re: Watermark all images in sub folders

Posted: 2017-03-06T02:59:45-07:00
by gogy
Hey guys, there is new problem.

When I check folder list in bash, I see all directories with space character, now having backslash. In WinSCP this backslash is not visible.

Please see the image with directory list
Image

Can I do something to remove those backslashes and how?

I tried with
mv 'GO\ TECH' 'GO TECH'
but no luck.

Even better would be if this could be renamed in batch.

Re: Watermark all images in sub folders

Posted: 2017-03-06T11:20:14-07:00
by GeeMack
gogy wrote: 2017-03-06T02:59:45-07:00When I check folder list in bash, I see all directories with space character, now having backslash. In WinSCP this backslash is not visible. [...] Can I do something to remove those backslashes and how?
Those backslashes don't actually exist as characters in the file or directory names. They're included in the display of the listing as a way to show where the spaces are in the names.

Re: Watermark all images in sub folders

Posted: 2017-03-06T11:29:15-07:00
by gogy
OK, thank you!

Is it possible by default to hide those backslashes if they don't exist?

Re: Watermark all images in sub folders

Posted: 2017-03-06T12:35:27-07:00
by snibgo
Is this a question about ImageMagick?

If you want to rename directories, replacing spaces with something else, that's a scripting question. (In Windows, just do a "ren" inside a "for /D" loop.)

Re: Watermark all images in sub folders

Posted: 2017-03-06T12:37:42-07:00
by gogy
Hi, it is not question about ImageMagick :) but I see you guys rock!

It is CentOS (bash). Thank you anyway

Re: Watermark all images in sub folders

Posted: 2017-09-08T18:30:59-07:00
by liyucmh
fmw42 wrote: 2013-05-17T18:24:45-07:00 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
this command works well, thanks
I have three more question, pls help
①can it use composite? such as "composite -gravity southeast -geometry +10+0 -dissolve 100" these kind of command
②can it overwrite original file, do not create new file?
③if file name contain special characters(I use centos vm, and use it to process windows file) such as 1 (8).jpg, it show error like:
file=(8).jpg
convert: unable to open image `(8).jpg': No such file or directory @ error/blob.c/OpenBlob/2709.
convert: no images defined `info:' @ error/convert.c/ConvertImageCommand/3210.
convert: unable to open image `(8).jpg': No such file or directory @ error/blob.c/OpenBlob/2709.
convert: no images defined `_new.jpg' @ error/convert.c/ConvertImageCommand/3210.

how the write the batch command, thanks~

Re: Watermark all images in sub folders

Posted: 2017-09-10T15:08:33-07:00
by fmw42
You should be able to use composite, but then you cannot use -draw. But composite can composite one image over another with dissolve. Did you try your command?

You can overwrite files. Did you try it?

I have no idea why you get and error with (8).jpg

Some OS systems do not like special characters such as ( or ). Also note that ( 8 is a smiley of you remove my space. Have you tried putting double quotes about your filenames, such as "(8).jpg"?

Re: Watermark all images in sub folders

Posted: 2017-09-13T21:08:07-07:00
by liyucmh
fmw42 wrote: 2017-09-10T15:08:33-07:00 You should be able to use composite, but then you cannot use -draw. But composite can composite one image over another with dissolve. Did you try your command?

You can overwrite files. Did you try it?

I have no idea why you get and error with (8).jpg

Some OS systems do not like special characters such as ( or ). Also note that ( 8 is a smiley of you remove my space. Have you tried putting double quotes about your filenames, such as "(8).jpg"?
thanks for your reply, how to overwrite files, how to write the command, pls help