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

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
myicq
Posts: 29
Joined: 2012-04-11T04:23:21-07:00
Authentication code: 8675308

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

Post 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 ?
Last edited by myicq on 2018-05-24T03:47:26-07:00, edited 1 time in total.
myicq
Posts: 29
Joined: 2012-04-11T04:23:21-07:00
Authentication code: 8675308

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

Post by myicq »

Desired results (simplified) showing 10px 1 and 2 px wide.

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

Image
https://postimg.cc/image/kbnqi240r/
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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
snibgo's IM pages: im.snibgo.com
myicq
Posts: 29
Joined: 2012-04-11T04:23:21-07:00
Authentication code: 8675308

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

Post 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__

Post Reply