Page 1 of 1

How to make a "perfect grid" in BMP ? [solved]

Posted: 2018-05-24T01:40:05-07:00
by myicq
I have the need to programatically create a 1 bit BMP grid, meaning that I have the following parameters:
  • Width of file in pixel
  • Height of file in pixel
  • line width in pixel
  • line offset in pixel
I have done it in PerlMagick so far, but could use Imagemagick directly if need be, that's no problem.

BUT: when I create the grid, I get these odd squares where lines meet, and I get strange artifacts between lines if line width is different from 2.
This is a zoom of the squares, each line is 10 pixel apart, width = 2 pixel.

Image

Example of the 15x1 version, with artifacts between lines:
Image

How do I create a "pixel perfect grid" in Imagemagic ? Is it possible ?

Re: How to make a "perfect grid" in BMP ?

Posted: 2018-05-24T01:59:30-07:00
by myicq
Desired results (simplified) showing 10px 1 and 2 px wide.

Image
https://postimg.cc/image/bgmw7gmx7/

Image
https://postimg.cc/image/kbnqi240r/

Re: How to make a "perfect grid" in BMP ?

Posted: 2018-05-24T02:54:31-07:00
by snibgo
wrote:BUT: when I create the grid, I get these odd squares where lines meet ...
If you show the command you used, perhaps we can diagnose the problem.

I draw grids with a script that draws a number of lines: Cylinders: grid.bat

Re: How to make a "perfect grid" in BMP ? [solved]

Posted: 2018-05-24T03:56:03-07:00
by myicq
Believe I figured it out, via a small trip through PPM format. I marked this thread as solved.

The solution is to make sure the lines are not Antialiased.

Thanks for response and link, snibgo

In case anyone can use the script:

Code: Select all

## ------------ perl script for making grids ----------------------
##
use Image::Magick;
use Getopt::Long;
use Pod::Usage;

my $width = 5000;
my $height = 1200;
my $lineoffset = 600;  
my $linewidth = 4;
my $man = 0;

$res = GetOptions("width|w=i" => \$width,   # width
                  "height|h=i" => \$height,
                  "offset|o=i" => \$lineoffset,
                  "line|l=i"   => \$linewidth  );
                  
my($image, $p, $q);

pod2usage(-verbose => 2) if $helpme;


print STDERR <<DEB;
========================================
Parameters:
-------------
Width  = $width 
Height = $height
Offset = $lineoffset
Line   = $linewidth
Font   = $fontheight
========================================
DEB

       # pixels between each line
my $cur_x = 0;                # current line pos


$image = Image::Magick->new (size=>$width . "x" . $height);
$image->Read('xc:white');


# VERTICAL LINES
while ($cur_x < $width) {
    $lineno = 0;
       
        my $pts =  $cur_x . ','  . 1;
        my $pts2 = $cur_x . ',' . $height;
        print STDERR $pts . ' ' . $pts2;
        print STDERR "\n";
        $image->Draw(
            primitive=>'line',
            points=> $pts . ' ' . $pts2,
            stroke=>'black',
            strokewidth=>$linewidth,
            antialias=>false		# <<< important to include this
          );
        $cur_x += $lineoffset;
}

# HORIZONTAL LINES
my $cur_y = 0;
while ($cur_y < $height) {
        $lineno = 0;     
        my $pts =  1 . ','  . $cur_y;
        my $pts2 = $width . ',' . $cur_y;
        print STDERR $pts . ' ' . $pts2;
        print STDERR "\n";
        $image->Draw(
            primitive=>'line',
            points=> $pts . ' ' . $pts2,
            stroke=>'black',
            strokewidth=>$linewidth,
            antialias=>false        # <<< important to include this
          );
        $cur_y += $lineoffset;
}


$r = $image->Set(			
        colors=>"2",
        depth=>"0",
        antialias=>"false",
        type=>'bilevel',
			);
	warn "$r" if "$r";

my $ofn = "grid_w".$width . "_h" .$height. "_" .$lineoffset."x".$linewidth.".bmp";
print STDERR "Writing to $ofn\n";
$image->Write($ofn);
undef $image;

__END__