Page 1 of 1

How to split image by simple horizontal line

Posted: 2012-04-14T20:11:32-07:00
by nabedge
"original.bmp" has only one line. The line is horizontal, straight, red line but position is inconstant.
Image

result images :
a.bmp:Image
b.bmp:Image

How to split like this case ?

Re: How to split image by simple horizontal line

Posted: 2012-04-14T22:13:00-07:00
by fmw42
In unix, this will work. You have to find the location of the red line and the image's width and height and then compute the crop coordinates.

see string format at http://www.imagemagick.org/script/escape.php


infile="1original.bmp"
inname=`convert $infile -format "%t" info:`
width=`convert $infile -format "%w" info:`
height=`convert $infile -format "%h" info:`
yval=`convert $infile -fill black +opaque red -trim -format "%O" info: | cut -d+ -f3`
h1=$((yval-1))
y2=$((yval+1))
h2=$((height-yval))
convert $infile -crop ${width}x${h1}+0+0 +repage ${inname}_top.png
convert $infile -crop ${width}x${h2}+0+${y2} +repage ${inname}_bottom.png


For windows user, the syntax is much different for getting variables. See http://www.imagemagick.org/Usage/windows/


Note:
convert $infile -fill black +opaque red -trim -format "%O" info: | cut -d+ -f3

This line converts every color in the image to black except red, then trims it down to the 1 red line keeping the virtual canvas information. Then it extracts the offset information for the red line relative to the whole image, then it extracts and saves to the variable just the y offset.