【问题标题】:ImageMagick exract non-rectangular region with transparent backgroundImageMagick 提取具有透明背景的非矩形区域
【发布时间】:2018-02-04 03:54:18
【问题描述】:

基于对以下问题Programmatically divide scanned images into separate images 的回答,我现在能够提取矩形区域。

是否可以修改以下 ImageMagick 脚本:

infile="image.png"
inname=`convert -ping $infile -format "%t" info:`
OLDIFS=$IFS
IFS=$'\n'
arr=(`convert $infile -blur 0x5 -auto-level -threshold 99% -type bilevel +write tmp.png \
-define connected-components:verbose=true \
-connected-components 8 \
null: | tail -n +2 | sed 's/^[ ]*//'`)
num=${#arr[*]}
IFS=$OLDIFS
for ((i=0; i<num; i++)); do
#echo "${arr[$i]}"
color=`echo ${arr[$i]} | cut -d\  -f5`
bbox=`echo ${arr[$i]} | cut -d\  -f2`
echo "color=$color; bbox=$bbox"
if [ "$color" = "gray(0)" ]; then
convert $infile -crop $bbox +repage -fuzz 10% -trim +repage ${inname}_$i.png
fi
done

能够提取与 tmp.png 中的黑色区域完全匹配的非矩形区域:

例如,为提取的图像的其余与黑色区域无关的矩形区域添加透明背景。

【问题讨论】:

    标签: image-processing imagemagick


    【解决方案1】:

    是的,我的 ImageMagick 脚本可以修改如下:

    infile="image.png"
    inname=`convert -ping $infile -format "%t" info:`
    OLDIFS=$IFS
    IFS=$'\n'
    arr=(`convert $infile +repage -blur 0x7 -auto-level -negate -threshold 2% -negate -type bilevel +write tmp.png \
    -define connected-components:verbose=true \
    -connected-components 8 \
    null: | tail -n +2 | sed 's/^[ ]*//'`)
    num=${#arr[*]}
    IFS=$OLDIFS
    for ((i=0; i<num; i++)); do
    echo "${arr[$i]}"
    color=`echo ${arr[$i]} | cut -d\  -f5`
    bbox=`echo ${arr[$i]} | cut -d\  -f2`
    echo "color=$color; bbox=$bbox"
    if [ "$color" = "gray(0)" ]; then
    convert tmp.png -crop $bbox +repage -fuzz 10% -trim +repage -alpha copy -channel a -negate +channel ${inname}_$i.png
    fi
    done
    

    以下结果是透明的,所以外面显示为白色。但是如果你下载它们,你会看到背景是透明的。

    【讨论】:

    • 非常感谢您一如既往的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 2023-03-24
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多