How to split image by simple horizontal line

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
nabedge
Posts: 1
Joined: 2012-04-14T19:48:53-07:00
Authentication code: 8675308

How to split image by simple horizontal line

Post 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 ?
User avatar
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

Post 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.
Post Reply