Optimizing Bash Script

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
arcking
Posts: 10
Joined: 2011-06-10T11:29:39-07:00
Authentication code: 8675308

Optimizing Bash Script

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Optimizing Bash Script

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Optimizing Bash Script

Post 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!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
arcking
Posts: 10
Joined: 2011-06-10T11:29:39-07:00
Authentication code: 8675308

Re: Optimizing Bash Script

Post 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!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Optimizing Bash Script

Post 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
arcking
Posts: 10
Joined: 2011-06-10T11:29:39-07:00
Authentication code: 8675308

Re: Optimizing Bash Script

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Optimizing Bash Script

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Optimizing Bash Script

Post 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'.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Optimizing Bash Script

Post 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.
Post Reply