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.
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!
crop a rotated box out of an image
-
- 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
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.
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?
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
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
Re: crop a rotated box out of an image
snibgo, thanks! this is exactly what I was looking for.
Re: crop a rotated box out of an image
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
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
-
- 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
The "+" form will create a canvas offset, and you need to subtract this from the x-offset I give above. For example:
(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 )
Code: Select all
%IM%convert in.png +distort SRT 0,202,85 -format "%%X" -write info: +repage out.png
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
Re: crop a rotated box out of an image
once I figured out the bash syntax, this works well. Thanks again for a quick response.