You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ImageMagick的convert命令中嵌入if-else控制逻辑?

简化ImageMagick脚本的条件逻辑实现

问题背景

在Cygwin环境下使用ImageMagick 7.0.10-61编写的脚本中,if和else块的convert命令几乎完全重复,仅两处参数存在差异:

  • 当$val > 300时,使用:\( -size 70x50 xc:white \) -geometry +851+0
  • 否则使用:\( -size 30x40 xc:white \) -geometry +840+0

尝试在convert命令内部直接嵌入shell的if-else逻辑失败,需要可行的简化方案。

可行解决方案

方案1:提前定义差异参数变量

将差异部分提取为shell变量,在convert命令中引用,避免重复代码:

for ((i=0; i<$num; i++)); do
    val=300
    
    # 根据val的值设置差异参数
    if [[ $val -gt 300 ]]; then
        size_param="70x50"
        geometry_param="+851+0"
    else
        size_param="30x40"
        geometry_param="+840+0"
    fi
    
    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 ${size_param} xc:white \) -geometry ${geometry_param} -composite -bordercolor "#4B8DF8" -border 2x2 \) \
                \( -size 898x437! mpr:img -crop "${coords[2]}" +repage \( -size ${size_param} xc:white \) -geometry ${geometry_param} -composite -bordercolor "#4B8DF8" -border 2x2 \) \
                +smush +12 -resize 1780x441! 
            \) \
            -gravity center -smush +10  \
            output_image$i.png
done

方案2:内嵌shell条件表达式

直接在convert命令的参数位置使用shell的子命令执行条件判断,返回对应参数:

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 \
            \( \
                \( -size 898x437! mpr:img -crop "${coords[1]}" +repage \
                   \( -size $( [[ $val -gt 300 ]] && echo "70x50" || echo "30x40" ) xc:white \) \
                   -geometry $( [[ $val -gt 300 ]] && echo "+851+0" || echo "+840+0" ) -composite -bordercolor "#4B8DF8" -border 2x2 \) \
                \( -size 898x437! mpr:img -crop "${coords[2]}" +repage \
                   \( -size $( [[ $val -gt 300 ]] && echo "70x50" || echo "30x40" ) xc:white \) \
                   -geometry $( [[ $val -gt 300 ]] && echo "+851+0" || echo "+840+0" ) -composite -bordercolor "#4B8DF8" -border 2x2 \) \
                +smush +12 -resize 1780x441! 
            \) \
            -gravity center -smush +10  \
            output_image$i.png
done

失败原因说明

之前的尝试错误地将shell的if-else控制语句直接插入convert命令的参数列表中,shell会将if识别为独立命令而非convert的参数,导致语法错误。必须让shell在解析convert命令前完成条件判断,将结果作为参数传递给convert。

内容的提问来源于stack exchange,提问作者Rasec Malkic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 08:52:02