ImageMagick提取具有透明背景的非矩形区域 [英] ImageMagick exract non-rectangular region with transparent background

查看:255
本文介绍了ImageMagick提取具有透明背景的非矩形区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据以下问题的答案



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



解决方案

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

  infile = image.png
inname =`convert -ping $ infile -format%tinfo:`
OLDIFS = $ IFS
IFS = $'\ n'
arr =(`convert $ infile + repage -blur 0x7 -auto-level -negate -t​​hreshold 2%-negate -t​​ype 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆