draw & rotate or affine path

PerlMagick is an object-oriented Perl interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning PerlMagick.
Post Reply
bigtable
Posts: 14
Joined: 2008-01-23T22:37:27-07:00

draw & rotate or affine path

Post by bigtable » 2011-07-20T09:26:49-07:00

Hello, I'm having some trouble with Draw() with rotate or affine. I'm attempting to add a path to the end of line and rotate the path according to the angle of the said line:

PerlMagick ImageMagick 6.2.8 10/20/10 Q16 file:/usr/share/ImageMagick-6.2.8/doc/index.html

$detail->Draw(stroke=>"red", primitive=>'path', rotate=>45, strokewidth=>1, points=>"$path");
Does not draw anything. If I remove rotate, it draws the path fine.

If I try to convert the angle to an array for affine:
$detail->Draw(stroke=>"red", primitive=>'path', affine=>[$affine], strokewidth=>1, points=>"$path");
this draws the path but does not alter it.

If I put the affine data in an array,
$detail->Draw(stroke=>"red", primitive=>'path', affine=>\@affine, strokewidth=>1, points=>"$path");
does not draw anything -- I put this here only because I'm not 100% sure about the formatting.

Any pointers are very much welcome -- thanks in advance for your time!

User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: draw & rotate or affine path

Post by anthony » 2011-07-20T18:10:25-07:00

The order of operations may be important. Not certain. Try placing primitive=>'path' just before points=>"$path" at the end.

I also suggest you look for errors after every image process. That may show up what is wrong.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

bigtable
Posts: 14
Joined: 2008-01-23T22:37:27-07:00

Re: draw & rotate or affine path

Post by bigtable » 2011-07-21T14:20:57-07:00

Thanks for your reply -- after some further testing I found the problem isn't with ImageMagick, it's with my stupid assumption of how it should work. :) Rotate works as advertised. After realizing that, I adapted your affine_distort program but my results aren't quite right.

I have a line from 300,175 300.000,225.000. In my example I'm taking the center of the line, rotating it 90 degrees and wanting to place it so that it would form a plus sign.

Here's my affine matrix based on the following input to affine_distort:
300,200 1,1 90 300,200
0.000000,1.000000,-1.000000,0.000000,500.000000,-100.000000

And here's my resulting output
http://images.ewins.com/luke/draw.jpg

Close but not quite. Any more tips?

As always, thanks so much!

User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: draw & rotate or affine path

Post by anthony » 2011-07-21T21:23:02-07:00

affine_distort results are right.

I can confirm it using -distort SRT which takes the same arguments.

Code: Select all

convert null: -verbose -distort SRT '300,200 1,1 90 300,200' +verbose null:
Affine Projection:
-distort AffineProjection \
'0.000000,1.000000,-1.000000,0.000000,500.000000,-100.000000'
Affine Distort, FX Equivelent:
-fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;
xx=+0.000000*ii +1.000000*jj +100.000000;
yy=-1.000000*ii +0.000000*jj +500.000000;
p{ xx-page.x-.5, yy-page.y-.5 }' \
Note the formula in the -fx equivalent is the inverse affine distortion, as it is mapping destination coordinates to source coordinates, so it looks a little different.

Drawing the line both before (black) and after (red) using the command line...

Code: Select all

convert -size 600x400 xc: -draw '
        line 300,175 300.000,225.000 
        stroke red 
        affine 0.000000,1.000000,-1.000000,0.000000,500.000000,-100.000000
        line 300,175 300.000,225.000
    ' show:
and the result looks just fine. rotation at the center of the line.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: draw & rotate or affine path

Post by anthony » 2011-07-21T21:41:39-07:00

Going back to the original problem...

A 'path' is not just a string of numbers it is a SVG path of letters and numbers
http://www.imagemagick.org/Usage/draw/#paths

You need at least a 'M' to draw the path.

If you change the primative from path to line, then a string of 4 numbers is the "points" needed.

According to the document
http://www.imagemagick.org/script/perl-magick.php

The Draw argument affine needs an array of values, not a string of values, or a string in an array!

I think if you can get the example working we should add it to the Perlmagick/demos sub directory.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

bigtable
Posts: 14
Joined: 2008-01-23T22:37:27-07:00

Re: draw & rotate or affine path

Post by bigtable » 2011-07-22T05:50:24-07:00

RE: Paths, I'm very well versed with paths, thanks. I changed my test case to a line for simplicity.

Re: array values, OK, I'll try other methods to populate the the array but you can see it accepts the input and rotates it ... sending it an actual array does nothing and I have not tried an array of arrays yet but I will.

Thanks for your continued input on the matter and I'll reply to this thread with examples if I can get it to work.

bigtable
Posts: 14
Joined: 2008-01-23T22:37:27-07:00

Re: draw & rotate or affine path

Post by bigtable » 2011-07-22T07:07:33-07:00

$detail->Draw(stroke=>"red", strokewidth=>1, affine=>[$a1,$a2,$a3,$a4,$a5,$a6], primitive=>'line', points=>"$x1,$y1 $x2,$y2");
produces the results in the example image linked above. i've also tried this with the actual values for the affine with same results. affine accepts this as an array.

@af = ($a1, $a2, $a3, $a4, $a5, $a6);
$detail->Draw(stroke=>"red", strokewidth=>1, affine=>\@af, primitive=>'line', points=>"$x1,$y1 $x2,$y2");
produces the results in the example image linked above. affine accepts this as an array too.

I also tried an array of arrays in a couple different formats with no luck.

I know that the most basic format shown in the first line of this message produces different output when I change the values for $a5 and $a6. So by all appearances, the input is being delivered to the module.

But if you have any suggestions for alternative array structure for the affine input, I'd be happy to try them.

bigtable
Posts: 14
Joined: 2008-01-23T22:37:27-07:00

Re: draw & rotate or affine path

Post by bigtable » 2011-07-23T19:53:11-07:00

In case anyone is ever interested, my work-around for what I think is a bug in PerlMagick's Draw function for affine tranformations is to use the Math::Geometry::Planar module (cpan). You end up needing to convert any paths to polygons for the transformations. But I was able to rotate, mirror and move the polygons and subsequently draw them using PerlMagick.

I have a feeling that the affine computations in the draw function of PerlMagick would be faster that converting to polygons (if they worked). But beggars cannot be choosers in this case.

If anyone out there has a working example of a perlmagick draw() with affine that rotates on a defined point and moves the results to a define point, please post your version number and syntax.

I have no idea how the development of PerlMagick works but if there's a proper forum for reporting bugs and/or testing, I'd be happy to do that much.

Post Reply