Difference between revisions of "Perl GD/Pixel manipulation"
From Teknologisk videncenter
< Perl GD
m |
m |
||
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| | + | {| |
− | [[Image:Heth2.png|thumb| | + | |[[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 |
Latest revision as of 11:59, 23 January 2011
Playing with pixels. The script below makes this png picture.
#!/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;