"original.bmp" has only one line. The line is horizontal, straight, red line but position is inconstant.
result images :
a.bmp:
b.bmp:
How to split like this case ?
How to split image by simple horizontal line
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: How to split image by simple horizontal line
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.
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.