使用两个图像的比较作为 if 条件

Use comparison of two images as if condition

在我的 bash 脚本中,我想执行如下操作:

if  [ compare $image1 $image2 ] = 1; then // if images are graphically equal
rm $image2
fi

有什么想法吗?

您可以在 ImageMagick 中按如下方式执行此操作。 rmse 度量为 0 表示相同。比较写入 stderr,因此您需要添加 2>&1。比较结果为 0 (0),其中第一个在 0 到量子范围内,括号中的第二个在 0 到 1 范围内。所以我使用 cut 只获取第一个 0。比较输出设置为null:以便比较不会创建任何输出图像。注意 -d\

后面有两个空格
convert lena.png lena2.png

[ $(compare -metric rmse lena.png lena2.png null: 2>&1 | cut -d\  -f1) -eq 0 ] && rm lena2.png