Home > Enterprise >  bash image-magick: if condition inside the program call
bash image-magick: if condition inside the program call

Time:10-13

My bash script calls convert utility of the Image-magic to stack together several types of the input images depending on the provided condition:

if [ "$DETAILS" == 1 ]; then
#stack 3 types of images in vertical row
convert \( "${output}/${target}*.png" -bordercolor lightgoldenrod2 -border 0x2 -append \) \( "${output}/${dist}*.png" -bordercolor lightgoldenrod2 -border 0x2 -append \) \( "${output}/${angl}*.png" -bordercolor lightgoldenrod2 -border 0x2 -append \) -bordercolor lightgoldenrod2 -border 2x0  append -background white -alpha deactivate ${output}/HBONDS-summary.png
else
   #stack 1 type of images in vertical row
convert \( "${output}/${target}*.png" -bordercolor lightgoldenrod2 -border 0x2 -append \) -bordercolor lightgoldenrod2 -border 2x0  append -background white -alpha deactivate ${output}/HBONDS-summary.png
fi

Since the difference between two options is just the number of the blocks

\( .. \)

provided for convert, would it be possible rather to put the IF condition INSIDE the convert to simplify the script e.g. this could be a wrong bash syntax but the general idea may be:

 # Add two more blocks with ${dist} and ${angl} images if we match the condition:
convert \( "${output}/${target}*.png" -bordercolor lightgoldenrod2 -border 0x2 -append \) **!NB >>> if [ "$DETAILS" == 1 ];** then \( "${output}/${dist}*.png" -bordercolor lightgoldenrod2 -border 0x2 -append \) \( "${output}/${angl}*.png" **fi;** <<< -bordercolor lightgoldenrod2 -border 0x2 -append \) -bordercolor lightgoldenrod2 -border 2x0  append -background white -alpha deactivate ${output}/HBONDS-summary.png

CodePudding user response:

You can do something like this:

convert_extra_args=()
if [ "$DETAILS" == 1 ]; then
  convert_extra_args=(
    \( "${output}/${dist}*.png" -bordercolor lightgoldenrod2 -border 0x2 -append \)
    \( "${output}/${angl}*.png" -bordercolor lightgoldenrod2 -border 0x2 -append \)
  )
fi

convert \( "${output}/${target}*.png" -bordercolor lightgoldenrod2 -border 0x2 -append \) "${convert_extra_args[@]}" -bordercolor lightgoldenrod2 -border 2x0  append -background white -alpha deactivate ${output}/HBONDS-summary.png

CodePudding user response:

@oguzismail's solution is probably neater for building up a complex set of options, and for ease of quoting, but I just wanted to show another couple of possibilities. Note that these are bash-isms, i.e. specific to bash-like shells.


Output red box, but add blue box if DETAILS is 1:

DETAILS=0
magick -size 100x100 xc:red $( [ $DETAILS -eq 1 ] && echo "xc:blue  append" ) result.png 

and:

DETAILS=1
magick -size 100x100 xc:red $( [ $DETAILS -eq 1 ] && echo "xc:blue  append" ) result.png 

Another possibility:

DETAILS=""
printf "$DETAILS" | magick -size 100x100 xc:red $(cat) result.png

and:

DETAILS="xc:blue xc:yellow  append"
printf "$DETAILS" | magick -size 100x100 xc:red $(cat) result.png

I can't upload images for the moment - will do when imgur is playing ball again...

  • Related