Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   How to draw thin lines with Image::Magick? (http://www.velocityreviews.com/forums/t914588-how-to-draw-thin-lines-with-image-magick.html)

Josef Moellers 01-31-2011 02:09 PM

How to draw thin lines with Image::Magick?
 
Hi,

I'd like to draw lines only one pixel thin using Image::Magick.
The following draws lines two pixels thick:

#! /usr/bin/perl

use warnings;
use strict;
use Image::Magick;

my $img = Image::Magick->new(size => '22x22');
$img->ReadImage('xc:white');
$img->Draw(primitive => 'line',
linewidth => 1,
method => 'point',
points => '0,10 20,10',
fill => 'black',
antialias => 'false',
);
$img->Transparent(color => 'white');
$img->Write('thick.png');


What options are required to draw lines one pixel wide?
Besides "linewidth" I also tried "strikewidth" to no avail.

Josef
--
These are my personal views and not those of Fujitsu Technology Solutions!
Josef M÷llers (Pinguinpfleger bei FTS)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://de.ts.fujitsu.com/imprint.html

Steve M. 01-31-2011 06:53 PM

Re: How to draw thin lines with Image::Magick?
 
On Mon, 31 Jan 2011 15:09:22 +0100, Josef Moellers wrote:

> Hi,
>
> I'd like to draw lines only one pixel thin using Image::Magick. The
> following draws lines two pixels thick:
>
> #! /usr/bin/perl
>
> use warnings;
> use strict;
> use Image::Magick;
>
> my $img = Image::Magick->new(size => '22x22');
> $img->ReadImage('xc:white');
> $img->Draw(primitive => 'line',
> linewidth => 1, <- ??????
> method => 'point',
> points => '0,10 20,10',
> fill => 'black',
> antialias => 'false',
> );
> $img->Transparent(color => 'white');
> $img->Write('thick.png');
>
>
> What options are required to draw lines one pixel wide? Besides
> "linewidth" I also tried "strikewidth" to no avail.
>
> Josef


Well...

$img -> Draw (
stroke => 'black',
primitive => 'line',
strokewidth => '1',
points => '1,10 20,10',
antialias => 'false',
);

should work.

I'm not sure where linewidth came from... strikewidth appears to be a
typo...

hth,

\s



--
\s

Josef Moellers 02-01-2011 03:17 PM

Re: How to draw thin lines with Image::Magick?
 
Am 1.2.2011 schrub Steve M.:

> On Mon, 31 Jan 2011 15:09:22 +0100, Josef Moellers wrote:
>
>> Hi,
>>
>> I'd like to draw lines only one pixel thin using Image::Magick. The
>> following draws lines two pixels thick:
>>
>> #! /usr/bin/perl
>>
>> use warnings;
>> use strict;
>> use Image::Magick;
>>
>> my $img = Image::Magick->new(size => '22x22');
>> $img->ReadImage('xc:white');
>> $img->Draw(primitive => 'line',
>> linewidth => 1, <- ??????
>> method => 'point',
>> points => '0,10 20,10',
>> fill => 'black',
>> antialias => 'false',
>> );
>> $img->Transparent(color => 'white');
>> $img->Write('thick.png');
>>
>>
>> What options are required to draw lines one pixel wide? Besides
>> "linewidth" I also tried "strikewidth" to no avail.
>>
>> Josef

>
> Well...
>
> $img -> Draw (
> stroke => 'black',
> primitive => 'line',
> strokewidth => '1',
> points => '1,10 20,10',
> antialias => 'false',
> );
>
> should work.



Thanks but ... the line is still 2 pixels wide.

> I'm not sure where linewidth came from... strikewidth appears to be a
> typo...


I found the linewidth in some documentation, so I just gave it a try.
The str*i*kewith was indeed a typo: strokewidth => 1 does not work either.

Thanks anyway,

Josef

NB My perl is 5.10.1, my perlmagick is "ImageMagick 6.5.7-8 2010-12-02
Q16 http://www.imagemagick.org"
--
These are my personal views and not those of Fujitsu Technology Solutions!
Josef M├Âllers (Pinguinpfleger bei FTS)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://de.ts.fujitsu.com/imprint.html

Josef Moellers 02-01-2011 04:30 PM

Re: How to draw thin lines with Image::Magick?
 
Am 1.2.2011 schrub Sherm Pendley:

> Josef Moellers <josef.moellers@ts.fujitsu.com> writes:
>
>> I'd like to draw lines only one pixel thin using Image::Magick.
>> The following draws lines two pixels thick:

>
> Fair warning: I'm not familiar with IM. This is just an idea based on
> my experience with other drawing APIs, so I apologize in advance if it
> doesn't apply!
>
> Is IM's drawing based on pixel coordinates, or on image coordinates?
> For the former, X/Y coordinates denote the center points of pixels, so
> a one-pixel line is exactly one pixel thick.
>
> But, in image coordinates, X/Y denote the top left of a pixel, so that
> integer coordinates represent the lines between pixels. With antialias-
> ing, a one-pixel wide line would be rendered by coloring two adjacent
> pixels at half value. To draw a true one-pixel line in such systems,
> one must add 0.5 to both X and Y coordinates.



Now you did it! ;-)
Now the lines are 3 pixels wide!

It's certainly an idea worth investigating.

Thanks,

Josef
--
These are my personal views and not those of Fujitsu Technology Solutions!
Josef M÷llers (Pinguinpfleger bei FTS)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://de.ts.fujitsu.com/imprint.html

J. Gleixner 02-01-2011 05:59 PM

Re: How to draw thin lines with Image::Magick?
 
Josef Moellers wrote:
>> Josef Moellers <josef.moellers@ts.fujitsu.com> writes:
>>
>>> I'd like to draw lines only one pixel thin using Image::Magick.
>>> The following draws lines two pixels thick:



Looking at
http://www.smileygenerator.us/histor...ml#strokewidth

Try strokewidth => 0

Peter J. Holzer 02-01-2011 08:39 PM

Re: How to draw thin lines with Image::Magick?
 
On 2011-02-01 15:17, Josef Moellers <josef.moellers@ts.fujitsu.com> wrote:
> Am 1.2.2011 schrub Steve M.:
>> On Mon, 31 Jan 2011 15:09:22 +0100, Josef Moellers wrote:
>>> I'd like to draw lines only one pixel thin using Image::Magick. The
>>> following draws lines two pixels thick:

[...]
>>> $img->Draw(primitive => 'line',
>>> linewidth => 1, <- ??????
>>> method => 'point',
>>> points => '0,10 20,10',
>>> fill => 'black',
>>> antialias => 'false',
>>> );

[...]
>>> What options are required to draw lines one pixel wide?

[...]
>> $img -> Draw (
>> stroke => 'black',
>> primitive => 'line',
>> strokewidth => '1',
>> points => '1,10 20,10',
>> antialias => 'false',
>> );
>>
>> should work.

>
>
> Thanks but ... the line is still 2 pixels wide.


Both variants work for me with Image::Magick 6.6.0 (Debian package
perlmagick 8:6.6.0.4-3).

hp


Steve M. 02-01-2011 09:24 PM

Re: How to draw thin lines with Image::Magick?
 
On Tue, 01 Feb 2011 16:17:15 +0100, Josef Moellers wrote:

> Am 1.2.2011 schrub Steve M.:
>
>>
>> Well...
>>



>> $img -> Draw (
>> stroke => 'black',
>> primitive => 'line',
>> strokewidth => '1',
>> points => '1,10 20,10',
>> antialias => 'false',
>> );
>>
>> should work.

>
>
> Thanks but ... the line is still 2 pixels wide.
>



>
> Josef
>
> NB My perl is 5.10.1, my perlmagick is "ImageMagick 6.5.7-8 2010-12-02
> Q16 http://www.imagemagick.org"



Hmmm...... That code worked with my Perl (5.10.1) and ImageMagick....

Well, if you didn't care about fiddling with anti-alias nonsense, I
suppose you could:

for( 1..20 ){
$img->Set("pixel[$_,10]"=>'black');
}


Not efficient perhaps.

hth,

--
\s

Josef Moellers 02-02-2011 09:16 AM

Re: How to draw thin lines with Image::Magick?
 
Am 2.2.2011 schrub Steve M.:

> On Tue, 01 Feb 2011 16:17:15 +0100, Josef Moellers wrote:
>
>> Am 1.2.2011 schrub Steve M.:
>>
>>>
>>> Well...
>>>

>
>
>>> $img -> Draw (
>>> stroke => 'black',
>>> primitive => 'line',
>>> strokewidth => '1',
>>> points => '1,10 20,10',
>>> antialias => 'false',
>>> );
>>>
>>> should work.

>>
>>
>> Thanks but ... the line is still 2 pixels wide.
>>

>
>
>>
>> Josef
>>
>> NB My perl is 5.10.1, my perlmagick is "ImageMagick 6.5.7-8 2010-12-02
>> Q16 http://www.imagemagick.org"

>
>
> Hmmm...... That code worked with my Perl (5.10.1) and ImageMagick....
>
> Well, if you didn't care about fiddling with anti-alias nonsense, I
> suppose you could:
>
> for( 1..20 ){
> $img->Set("pixel[$_,10]"=>'black');
> }
>
>
> Not efficient perhaps.


It wouldn't have been a problem, I stumbled across this when I tried to
draw a small 22x22 "dia" shape.
But it helped me find my problem. It's somewhere between the keyboard
and the chair: I was fooled by gimp!

When I tried to draw this pixel-by-pixel line, I found that when I
looked at it with gimp, gimp would show two different coordinates when I
moved the cursor from one side of the line to the other, which I
definitely thought could not be the case with "Set(pixel ...)"!
So I tried to select one pixel and ... indeed the line is only one pixel
wide! A 22x22 image with 22 vertical lines shows this to be correct.
Even with $img->Draw(...) the line is only one pixel wide!

Thanks for all the help and suggestions.

Josef
--
These are my personal views and not those of Fujitsu Technology Solutions!
Josef M├Âllers (Pinguinpfleger bei FTS)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://de.ts.fujitsu.com/imprint.html


All times are GMT. The time now is 01:23 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.