in reply to Generating image (map)

This is very easy using GD. The data is random and the plot greyscale, but this does pretty much exactly what you've described:

#! perl -slw use strict; use GD; sub rgb2n { unpack 'N', pack 'CCCC', 0, @_ } my @heights = map [ map int rand 256, 1 .. 500 ], 1 .. 500; my $img = GD::Image->new( 500, 500, 1 ); for my $y ( 0 .. $#heights ) { for my $x ( 0 .. $#{ $heights[ $y ] } ) { $img->setPixel( $x, $y, rgb2n( ( $heights[ $x][ $y ] ) x 3 ) ) +; } } binmode STDOUT; print $img->png;

Run it as theScript > a.png and then load the png into your favorite image viewer.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"I'd rather go naked than blow up my ass"

Replies are listed 'Best First'.
Re^2: Generating image (map)
by bobo (Novice) on Mar 19, 2010 at 22:13 UTC
    Thanks, that is exactly what I was looking for... :)

      If you'd prefer a color map instead of greyscale, ColorRamp1785 may be of interest.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Greyscale is ok, but if I use greyscale for map I can still mark crossed path in lets say red, right?
Re^2: Generating image (map)
by bobo (Novice) on Mar 19, 2010 at 23:54 UTC
    It seems I'm a bit of a moron... For some reason I can't install GD on my system...
    First I tried using cpan shell... It reported some errors and then stopped...
    Then I tried to install GD from source going over readme instructions for GD... Somehow I failed that too...
    Then I tried to find a library readme file said usually causes problems... I didn't found it...
    I know I'm doing something terribly wrong, but I don't know what... :(
    Can you tell me what am I doing wrong?
      Can you tell me what am I doing wrong?

      On the basis of that description, no.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Error reports that there is no png function in GD::Image... I looked it up and there really isn't any... But http://search.cpan.org/~lds/GD-2.44/GD.pm says that there should be... Actually they have example same as your code (which I just copied)

        P.S. i forgot to ask... what does rgb2n function do? and will it work without it?
        P.P.S. still not working, but i did it manualy with printing in .ppm file
        Yes, I figured... Forgot to put error messages...
        Meanwhile, I figured it out... I think I did (about to test it)... Stupid mistakes... So stupid...
        Thanks anyway, I'll try not to bother you anymore... :)

        P.S. It works, but reports error:
        "Can't locate object method "png" via package "GD::Image" at ./uilab.pl line 21" :-/
        P.P.S Code is c/p of your first reply