ImageMagick提取具有透明背景的非矩形区域 [英] ImageMagick exract non-rectangular region with transparent background
问题描述
根据以下问题的答案
例如,为提取的矩形区域的其余部分添加透明背景与黑色区域无关的图像。
是的,我的ImageMagick脚本可以修改如下:
infile = image.png
inname =`convert -ping $ infile -format%tinfo:`
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;我++)); do
echo$ {arr [$ i]}
color =`echo $ {arr [$ i]} | cut -d\ -f5`
bbox =`echo $ {arr [$ i]} | cut -d\ -f2`
echocolor = $ color; bbox = $ bbox
if [$ color=gray(0)];然后
转换tmp.png -crop $ bbox + repage -fuzz 10%-trim + repage -alpha copy -channel a -negate + channel $ {inname} _ $ i.png
fi
完成
以下结果是透明的,因此外部显示为白色。但是如果你下载它们,你会发现背景是透明的。
Based on the answer to the following question Programmatically divide scanned images into separate images right now I able to extract rectangular region.
Is it possible to modify the following ImageMagick script:
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
to be able to extract non-rectangular region that exactly matches the black regions in the tmp.png:
For example, add a transparent background for the rest of the rectangular region of the extracted image that is not related to the black region.
Yes, my ImageMagick script can be modified as follows:
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
The following results are transparent, so show white for the outside. But if you download them, you will see that the background is transparent.
这篇关于ImageMagick提取具有透明背景的非矩形区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!