我在
ImageMagick 7.0.10-61/Cygwin
中有一个脚本,如下所示,它从主图像中裁剪一些区域并以定义的方式合并。
for ((i=0; i<$num; i++)); do
val=300
if [[ $val -gt 300 ]]; then
convert "$input_image" +repage -write mpr:img -delete 0--1 -background none \
\( \( -size 1600x120! xc:"#00377B" -fill white -font Calibri-Bold -pointsize 90 -gravity center -annotate +0+0 "${coords[0]}" \) \) \
\( \
\( -size 426x50 xc:"#4FA7FF" -fill black -font Calibri-Bold -pointsize 42 -gravity center -annotate +0+0 "TITLE A" \) \
\( -size 426x50 xc:"#4FA7FF" -fill black -font Calibri-Bold -pointsize 42 -gravity center -annotate +0+0 "TITLE B" \) \
+smush +440 \
\) \
-smush +60 +gravity \
\( \
\( -size 898x437! mpr:img -crop "${coords[1]}" +repage \( -size 70x50 xc:white \) -geometry +851+0 -composite -bordercolor "#4B8DF8" -border 2x2 \) \
\( -size 898x437! mpr:img -crop "${coords[2]}" +repage \( -size 70x50 xc:white \) -geometry +851+0 -composite -bordercolor "#4B8DF8" -border 2x2 \) \
+smush +12 -resize 1780x441!
\) \
-gravity center -smush +10 \
output_image$i.png
else
convert "$input_image" +repage -write mpr:img -delete 0--1 -background none \
\( \( -size 1600x120! xc:"#00377B" -fill white -font Calibri-Bold -pointsize 90 -gravity center -annotate +0+0 "${coords[0]}" \) \) \
\( \
\( -size 426x50 xc:"#4FA7FF" -fill black -font Calibri-Bold -pointsize 42 -gravity center -annotate +0+0 "TITLE A" \) \
\( -size 426x50 xc:"#4FA7FF" -fill black -font Calibri-Bold -pointsize 42 -gravity center -annotate +0+0 "TITLE B" \) \
+smush +440 \
\) \
-smush +60 +gravity \
\( \
\( -size 898x437! mpr:img -crop "${coords[1]}" +repage \( -size 30x40 xc:white \) -geometry +840+0 -composite -bordercolor "#4B8DF8" -border 2x2 \) \
\( -size 898x437! mpr:img -crop "${coords[2]}" +repage \( -size 30x40 xc:white \) -geometry +840+0 -composite -bordercolor "#4B8DF8" -border 2x2 \) \
+smush +12 -resize 1780x441!
\) \
-gravity center -smush +10 \
output_image$i.png
fi
done
if
和else
里面的脚本除了这个块之外都是一样的
\( \
\( -size 898x437! mpr:img -crop "${coords[1]}" +repage \( -size 70x50 xc:white \) -geometry +851+0 -composite -bordercolor "#4B8DF8" -border 2x2 \) \
\( -size 898x437! mpr:img -crop "${coords[2]}" +repage \( -size 70x50 xc:white \) -geometry +851+0 -composite -bordercolor "#4B8DF8" -border 2x2 \) \
+smush +12 -resize 1780x441!
\) \
if
$val > 300
然后进行复合
\( -size 70x50 xc:white \) -geometry +851+0
否则可以制作这个复合材料
\( -size 30x40 xc:white \) -geometry +840+0
有没有办法在转换命令中包含
if-else
控件以简化脚本?像这样的东西(下面的尝试不起作用):
for ((i=0; i<$num; i++)); do
val=300
convert "$input_image" +repage -write mpr:img -delete 0--1 -background none \
\( \( -size 1600x120! xc:"#00377B" -fill white -font Calibri-Bold -pointsize 90 -gravity center -annotate +0+0 "${coords[0]}" \) \) \
\( \
\( -size 426x50 xc:"#4FA7FF" -fill black -font Calibri-Bold -pointsize 42 -gravity center -annotate +0+0 "TITLE A" \) \
\( -size 426x50 xc:"#4FA7FF" -fill black -font Calibri-Bold -pointsize 42 -gravity center -annotate +0+0 "TITLE B" \) \
+smush +440 \
\) \
-smush +60 +gravity \
if [[ $val -gt 300 ]]; then # BEGIN OF IF BLOCK
\( \
\( -size 898x437! mpr:img -crop "${coords[1]}" +repage \( -size 70x50 xc:white \) -geometry +851+0 -composite -bordercolor "#4B8DF8" -border 2x2 \) \
\( -size 898x437! mpr:img -crop "${coords[2]}" +repage \( -size 70x50 xc:white \) -geometry +851+0 -composite -bordercolor "#4B8DF8" -border 2x2 \) \
+smush +12 -resize 1780x441!
\) \
else # ELSE BLOCK
\( \
\( -size 898x437! mpr:img -crop "${coords[1]}" +repage \( -size 30x40 xc:white \) -geometry +840+0 -composite -bordercolor "#4B8DF8" -border 2x2 \) \
\( -size 898x437! mpr:img -crop "${coords[2]}" +repage \( -size 30x40 xc:white \) -geometry +840+0 -composite -bordercolor "#4B8DF8" -border 2x2 \) \
+smush +12 -resize 1780x441!
\) \
fi
-gravity center -smush +10 \
output_image$i.png
else
fi
done
如果您将转换命令动态构造为字符串,然后像这样稍后将其传入,会怎么样:
for ((i=0; i<$num; i++)); do
val=300
# Conditional part of the command
if [[ $val -gt 300 ]]; then
conditional_part="\
\( -size 898x437! mpr:img -crop \"${coords[1]}\" +repage \( -size 70x50 xc:white \) -geometry +851+0 -composite -bordercolor \"#4B8DF8\" -border 2x2 \) \
\( -size 898x437! mpr:img -crop \"${coords[2]}\" +repage \( -size 70x50 xc:white \) -geometry +851+0 -composite -bordercolor \"#4B8DF8\" -border 2x2 \) \
+smush +12 -resize 1780x441! \
"
else
conditional_part="\
\( -size 898x437! mpr:img -crop \"${coords[1]}\" +repage \( -size 30x40 xc:white \) -geometry +840+0 -composite -bordercolor \"#4B8DF8\" -border 2x2 \) \
\( -size 898x437! mpr:img -crop \"${coords[2]}\" +repage \( -size 30x40 xc:white \) -geometry +840+0 -composite -bordercolor \"#4B8DF8\" -border 2x2 \) \
+smush +12 -resize 1780x441! \
"
fi
# Full command with the conditional part included
convert "$input_image" +repage -write mpr:img -delete 0--1 -background none \
\( \( -size 1600x120! xc:"#00377B" -fill white -font Calibri-Bold -pointsize 90 -gravity center -annotate +0+0 "${coords[0]}" \) \) \
\( \
\( -size 426x50 xc:"#4FA7FF" -fill black -font Calibri-Bold -pointsize 42 -gravity center -annotate +0+0 "TITLE A" \) \
\( -size 426x50 xc:"#4FA7FF" -fill black -font Calibri-Bold -pointsize 42 -gravity center -annotate +0+0 "TITLE B" \) \
+smush +440 \
\) \
-smush +60 +gravity \
$conditional_part \
-gravity center -smush +10 \
"output_image$i.png"
done