Page 1 of 1

Optimizing Bash Script

Posted: 2011-07-13T10:19:42-07:00
by arcking
Is there anyway to optimize this script to be more efficient/faster? Each image takes .45s to 1s to process... I was hoping it would be a bit faster. (3Ghz, 16 core, 32GB of RAM) Thanks for any advice!

Code: Select all

#/bin/bash
echo -n "Enter original directory > "
echo -n
read odir
echo -n "Enter new directory > "
echo -n
read ndir
cd $odir
list=`ls`
for name in $list; do
`convert $name -resize "1000x1000" $ndir/$name`
`composite -gravity Center /home/web01/watermark.png $ndir/$name $ndir/$name`
echo "$name   |   $ndir/$name"
done

Re: Optimizing Bash Script

Posted: 2011-07-13T10:54:11-07:00
by fmw42
try making it one convert command and eliminate the composite command.

see
http://www.imagemagick.org/script/compose.php
and
http://www.imagemagick.org/Usage/compose/#watermark

Example:
convert \( logo: -resize 1000x1000 \) rose: -gravity center -geometry -100+0 \
-compose modulate -define compose:args=50,100 -composite tmp1.png


Also what is your input image format? Is it jpg? If so and if it is very big, you can use -define to read that part for resizing. See http://www.imagemagick.org/Usage/formats/#jpg_read

Re: Optimizing Bash Script

Posted: 2011-07-13T17:33:38-07:00
by anthony
Actually the real question is... Why are you using backquotes on the commands!!!!

The commands produce no output, so there is nothing to substutute, but as written you are also trying to execute the output that was generated! More typically backquotes is used only when you want to save the output into a variable.

Remove those back quotes!

Re: Optimizing Bash Script

Posted: 2011-07-18T08:38:01-07:00
by arcking
Thanks for the input. I ditched the backquotes, and switched to -thumbnail instead of -resize which seems to be faster. Is there a downside to that? I'm having trouble figuring out how to combine the covert and composite commands however. I tried this, but it doesn't work:

Code: Select all

convert $name -thumbnail "1000x1000" -compose modulate -gravity Center /home/web01/watermark.png $ndir/$name
Thanks!

Re: Optimizing Bash Script

Posted: 2011-07-18T11:10:27-07:00
by fmw42
Your syntax is out of order:

try

convert \( $name -thumbnail "1000x1000" \) /home/web01/watermark.png -gravity center \
-compose modulate -define compose:args=50,100 -composite $ndir/$name

-thumbnail will strip meta data but not profiles whereas -resize will not.

see
http://www.imagemagick.org/script/compose.php
and
http://www.imagemagick.org/Usage/compose/#watermark

http://www.imagemagick.org/script/comma ... #thumbnail

Re: Optimizing Bash Script

Posted: 2011-07-18T16:02:23-07:00
by arcking
fmw42 wrote:convert \( $name -thumbnail "1000x1000" \) /home/web01/watermark.png -gravity center \
-compose modulate -define compose:args=50,100 -composite $ndir/$name
That runs, but doesn't preserve the transparency in the watermark. Also, pardon my bash ignorance, but why the escape characters?

Re: Optimizing Bash Script

Posted: 2011-07-18T16:09:32-07:00
by fmw42
arcking wrote:
fmw42 wrote:convert \( $name -thumbnail "1000x1000" \) /home/web01/watermark.png -gravity center \
-compose modulate -define compose:args=50,100 -composite $ndir/$name
That runs, but doesn't preserve the transparency in the watermark. Also, pardon my bash ignorance, but why the escape characters?

The IM function only processes grayscale images as watermarks -- so it strips the alpha channel and converts the image to grayscale. See http://www.imagemagick.org/Usage/compose/#watermark

If you want to preserve the alpha channel, then you probably want to try -compose dissolve, though I am not sure of the alpha channel when blending? Otherwise one can tone down one image before a simple composite. I will have to experiment some unless you can provide links to your two input images. Do both you input images have transparency?

In unix, parens must be escaped with the \. On windows, the escapes for parens are not needed.


From your code, you are only doing a convert and composite, which can be combined easily as:

convert \( $name -resize "1000x1000" \) /home/web01/watermark.png -gravity Center -composite $ndir/$name

The parens keep the -resize from affecting the next image. Perhaps not needed, but safest to avoid v5 backward compatibility in v6 from affecting the result. see also http://www.imagemagick.org/Usage/basics/#parenthesis

This also seems to work to control the amount of the watermark image and keep transparency in both images:

convert \( $name -thumbnail "1000x1000" \) /home/web01/watermark.png -gravity center \
-compose dissolve -define compose:args=50,100 -composite $ndir/$name

so you can change the 50 to a smaller/larger amount to make the watermark less/more prominent.

see
http://www.imagemagick.org/Usage/compose/#dissolve

Re: Optimizing Bash Script

Posted: 2011-07-18T16:36:21-07:00
by anthony
Parenthesis are not needed.

in convert the IMv5 compatibility only applies up until the first image is read in.

After that it is strictly 'do options as you see them'.

Re: Optimizing Bash Script

Posted: 2011-07-18T16:46:27-07:00
by fmw42
anthony wrote:Parenthesis are not needed.

in convert the IMv5 compatibility only applies up until the first image is read in.

After that it is strictly 'do options as you see them'.

Good to know. Learn something new each day.

I guess my style is to keep things in logical groups, so I tend to use parens perhaps more than I need. It also helps with readability, especially if one puts in \ to go to the next line when the commands get complicated and there are lots of clones and processing on each clone.