http://www.perlmonks.org?node_id=1005127

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');

Replies are listed 'Best First'.
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