bash:Image Magic 转换的选项列表

问题描述 投票:0回答:1

我正在使用集成到 bash 脚本中的 Image-Magic 包的转换实用程序,目的是创建一个由许多图像组成的 10 x N 表,分为 10 种类型:

convert \( "${output}/type1.png" -append \) \( "${output}/type2.png" -append \) \( "${output}/type3.png" -append \) ... \( "${output}/type10.png" -append \)  +append -background white -alpha deactivate ${output}/project-summary.png

由于我的 bash 脚本中需要很长的一行,我想以以下方式对每个块(“${output}/typeN.png”-append)进行更多控制:

convert 
\( "${output}/type1.png" -append \) 
#\( "${output}/type2.png" -append \)  # e.g. this type is not included in summary
\( "${output}/type3.png" -append \) 
... 
if [ "${condition}" == 1 ]; then
\( "${output}/type10.png" -append \)  # e.g. this type is included only under condition
elif [ "${condition}" == 2 ]; then
\( "${output}/type11.png" -append \)  # e.g. this type is included only under condition
fi
+append -background white -alpha deactivate ${output}/project-summary.png

我正在尝试定义一个函数,其中包含与每个图像块相对应的元素列表,例如:

convert_control() {
opts=( \( "${output}/type1.png" -append \)  )
opts+=( \( "${output}/type2.png" -append \)  )
# opts+=( \( "${output}/type3.png" -append \)  ) # is not included
opts+=( \( "${output}/type4.png" -append \)  )
opts+=( \( "${output}/type5.png" -append \)  )
if [ "${condition}" == 1 ]; then
opts+=( \( "${output}/type10.png" -append \)  ) # included under condition
elif [ "${condition}" == 2 ]; then
opts+=( \( "${output}/type11.png" -append \)  )
fi
}

然后:

convert_control
convert "${opts[@]}" +append -background white -alpha deactivate ${output}/project-summary.png

还有其他可以在 bash 中以相同方式工作的解决方案吗?

bash imagemagick
1个回答
0
投票

将参数存储在数组中是正确的,但我不会为其定义函数。

也就是说,您可以使用以下方法使代码看起来更美观:

opts=(
    \( "$output/type1.png" -append \)
    \( "$output/type2.png" -append \)
#   \( "$output/type3.png" -append \) # not included in summary
    \( "$output/type4.png" -append \)
    \( "$output/type5.png" -append \)
)
case "$condition" in
    1) opts+=( \( "$output/type10.png" -append \) );;
    2) opts+=( \( "$output/type11.png" -append \) );;
esac
© www.soinside.com 2019 - 2024. All rights reserved.