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.