首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ImageMagick提取透明背景的非矩形区域

ImageMagick提取透明背景的非矩形区域
EN

Stack Overflow用户
提问于 2018-02-04 03:08:09
回答 1查看 105关注 0票数 0

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

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

代码语言:javascript
复制
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中的黑色区域完全匹配的非矩形区域:

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-04 03:54:18

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

代码语言:javascript
复制
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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48600988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档