bobo has asked for the wisdom of the Perl Monks concerning the following question:

Hello!

I have a bit of a problem and was hoping you could help me out.
I have a file in which I have n*m (integers, neither is bigger than 500) array of positive integers each representing a height at those coordinates in a map.
How can I generate image of that map? I tried to find a module for that but I found several modules for generating images but none of them really helps me.
All I could think of by myself was to manually generate plain .ppm format picture or even raw .ppm if that doesn't work.
Is there other way to do that? Some module from CPAN? I tried to find one, but ones I found don't help with my problem.
Also I have to edit that map later, so it would be nice if you can recommend a module for that also. :)
Thank You for Your time.

Best regards!

Replies are listed 'Best First'.
Re: Generating image (map)
by BrowserUk (Patriarch) on Mar 19, 2010 at 22:07 UTC

    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.
      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.
      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.