Perl GD/Pixel manipulation

From Teknologisk videncenter
< Perl GD
Revision as of 10:28, 23 January 2011 by Heth (talk | contribs)
Jump to: navigation, search

Playing with pixels. The script below makes this png picture.

Greyscale from 0 to 255
Using the equation @grey[ (sin($i/32)*$j) % 256 ]
#!/usr/bin/perl
 use GD;
 use GD::Image;

    # create a new image
    $im = new GD::Image(640,480);

    #Grey scale
    my @grey;
    for (my $i = 0; $i <=255; $i++) {
    	$grey[$i] = $im->colorAllocate($i,$i,$i);
    }
    print "Allocated ", $#grey+1, " colors\n";
 
    for( my $i=0; $i<=640-1 ;$i++ ) {
    	for( my $j=0; $j<=480-1;$j++ ) {
            $im->setPixel($i,$j,@grey[ ($i+$j) % 256 ]);
            #$im->setPixel($i,$j, $c[($i+$j)%4]);
        }
    }
    print "Der er ialt ", $im->colorsTotal(), " farver\n";


    # Convert the image to PNG and save it.
    open FILE,'> C:\temp\heth.png';
    binmode FILE;
    print FILE $im->png;