Difference between revisions of "Perl GD/Pixel manipulation"

From Teknologisk videncenter
Jump to: navigation, search
m
m
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
Playing with pixels. The script below makes this png picture.
 
Playing with pixels. The script below makes this png picture.
[[Image:Heth.png|thumb|left|400px|Greyscale from 0 to 255]]
+
{|
[[Image:Heth2.png|thumb|400px|Using the equation @grey[ (sin($i/32)*$j) % 256 ] ]]
+
|[[Image:Heth.png|thumb|left|300px|Greyscale from 0 to 255]]
 +
|[[Image:Heth2.png|thumb|300px|Using the equation @grey[ (sin($i/32)*$j) % 256 ] ]]
 +
|}
 
<source lang=perl>
 
<source lang=perl>
 
#!/usr/bin/perl
 
#!/usr/bin/perl
Line 31: Line 33:
 
     print FILE $im->png;
 
     print FILE $im->png;
 
</source>
 
</source>
{{source cli}}
 
 
[[Category:Perl]]
 
[[Category:Perl]]

Latest revision as of 11:59, 23 January 2011

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;