Beefy Boxes and Bandwidth Generously Provided by pair Networks BBQ
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

ImageMagick Point Query

by bensonmonk (Novice)
on Nov 22, 2012 at 12:13 UTC ( #1005127=perlquestion: print w/ replies, xml ) Need Help??
bensonmonk has asked for the wisdom of the Perl Monks concerning the following question:

I want to use ImageMagick to edit an existing image by making some selected points a specific colour.
I am trying to use the Draw command with the ‘point’ primitive. However, I can only get a black point regardless of what colour I am trying to set.
This is shown in the Perl code below where:
1. I draw a blue rectangle;
2. Try and set a number of points to white in a rectangle that partly overlaps the blue rectangle.
However, I get black points in the rectangle.
How can I set white (and any other colour) points?
use Image::Magick; my ($col, $img_width, $row, $img_height); # Create a new image my $im = Image::Magick->new(size => '400x300'); # Give it a white background $im->Read('xc:white'); # Put a rectangle in there somewhere $im->Draw(primitive => 'rectangle', points => '0,129 199,169', fill => 'blue', stroke => 'blue'); for($col = 50; $col < 100; $col ++ ) { for ($row = 150; $row < 200; $row ++ ) { $im->Draw(primitive => 'point', points => "$col,$row", stroke => 'white'); } } $im->Write('pointquery.png');

Comment on ImageMagick Point Query
Download Code
Re: ImageMagick Point Query
by zentara (Archbishop) on Nov 22, 2012 at 12:40 UTC
    How can I set white (and any other colour) points?

    Set the -fill option on the point primitives.

    #!/usr/bin/perl use Image::Magick; my ($col, $img_width, $row, $img_height); # Create a new image my $im = Image::Magick->new(size => '400x300'); # Give it a white background $im->Read('xc:white'); # Put a rectangle in there somewhere $im->Draw(primitive => 'rectangle', points => '0,129 199,169', fill => 'blue', stroke => 'blue'); for($col = 50; $col < 100; $col ++ ) { for ($row = 150; $row < 200; $row ++ ) { $im->Draw( primitive => 'point', points => "$col,$row", stroke => 'white', fill => 'white' # you missed this fill right here!!!!!!!!!! +! ); } } $im->Write("$0.png");

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Exactly!! Many thanks.
Re: ImageMagick Point Query
by ColonelPanic (Friar) on Nov 22, 2012 at 13:51 UTC

    Just a quick usage note: in Perl there is almost always an easier solution than C-style for loops

    for my $col (50..99) { for my $row (150..199) { #same code here } }

    It's a small point, but in my experience incorrectly handled iterator variables are a common source of mistakes. Letting Perl take care of that means fewer headaches.



    When's the last time you used duct tape on a duct? --Larry Wall

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1005127]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (11)
As of 2014-04-23 22:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (556 votes), past polls