crop a rotated box out of an image

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
utnuc
Posts: 12
Joined: 2014-09-17T06:41:13-07:00
Authentication code: 6789

crop a rotated box out of an image

Post by utnuc »

Hello, I am having a hard time wrapping my head around this one.

I have an image that the user is able to draw a 3px line across, and that line is extended to the edges of the image. What I'd like to do is crop the image to the pixels under the line and rotate them to have a 3px width portrait-oriented image. Image
I can get the exact coordinates of the user-drawn line. I've been playing around with -rotate, as it's easy math to calculate the angle of the line. But it comes time to crop the rotated image, I can't figure out how to find the line's position once again. Cropping first doesn't solve the problem.

Any thoughts from a brilliant person?
Thanks in advance!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: crop a rotated box out of an image

Post by snibgo »

If I understand you: the user specifies a yellow line. You want to rotate to make the line vertical, and then find the x-offset of this line. Is that right?

If so, then you could do some complex geometric calculation, or let IM do those for you. I will do the second.

There are different way to rotate an image. "-distort SRT" is useful here. see http://imagemagick.org/script/command-l ... hp#distort

Suppose you know two coordinates of the line. In this example, two coordinates are (249,56) and (534,388).

The rotation angle to make these vertical is atan2(534-249,388-56) = 40.64 degrees.

Give "-distort SRT" three parameters: one x,y coordinate, and the angle.

Code: Select all

convert i-VVNQzqm.jpg -distort SRT 249,56,40.64 r.png
r.png now has the vertical yellow line at x=249.

This does crop some of the image away. Using "+distort" cures this, but makes life slightly more complicated.

Does that answer the question?
snibgo's IM pages: im.snibgo.com
utnuc
Posts: 12
Joined: 2014-09-17T06:41:13-07:00
Authentication code: 6789

Re: crop a rotated box out of an image

Post by utnuc »

snibgo, thanks! this is exactly what I was looking for.
utnuc
Posts: 12
Joined: 2014-09-17T06:41:13-07:00
Authentication code: 6789

Re: crop a rotated box out of an image

Post by utnuc »

snibgo,

I have run into an issue with this code. Specifically the cropping that happens with -distort when the line goes primarily horizontal (0,202) (792,280) is unacceptable. Is there any way to use +distort and know the location of the line?

Ben
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: crop a rotated box out of an image

Post by snibgo »

The "+" form will create a canvas offset, and you need to subtract this from the x-offset I give above. For example:

Code: Select all

%IM%convert in.png +distort SRT 0,202,85 -format "%%X" -write info: +repage out.png
(Windows BAT syntax; adjust for other shells.)

For example, if this gives "-268" then you calculate 0-(-268) = +268, and this is the x-offset.

(For format escapes, see http://www.imagemagick.org/script/escape.php )
snibgo's IM pages: im.snibgo.com
utnuc
Posts: 12
Joined: 2014-09-17T06:41:13-07:00
Authentication code: 6789

Re: crop a rotated box out of an image

Post by utnuc »

once I figured out the bash syntax, this works well. Thanks again for a quick response.
Post Reply