I partially blame my math teacher for this stuff... He forced us poor students into similar effects in Mathcad and also claimed Python is loads nicer than Perl :)
Note: This only works for grayscale (Black & White) images. Color palettes for color images is much more complex.
Update: fixed bug in emboss and added invert and level.
#!/usr/bin/perl -w use strict; use GD; sub blur { my $image = GD::Image->newFromJpeg("image.jpg"); my ($width,$height) = $image->getBounds(); my $image2 = GD::Image->new($width,$height); for my $w (2..$width-1){ for my $h (2..$height-1){ my $index = $image->getPixel($w,$h); my (@rgb) = $image->rgb($index); my $blur = ( ($image->rgb($image->getPixel($w-1,$h-1)))[0]+ ($image->rgb($image->getPixel($w-1,$h)))[0]+ ($image->rgb($image->getPixel($w-1,$h+1)))[0]+ ($image->rgb($image->getPixel($w,$h-1)))[0]+ ($image->rgb($image->getPixel($w,$h)))[0]+ ($image->rgb($image->getPixel($w,$h+1)))[0]+ ($image->rgb($image->getPixel($w+1,$h-1)))[0]+ ($image->rgb($image->getPixel($w+1,$h)))[0]+ ($image->rgb($image->getPixel($w+1,$h+1)))[0])/9; $blur = sprintf("%.f",$blur); $blur = $blur > 255 ? 255 : ($blur < 0 ? 0 : $blur ); $index = $image2->colorExact($blur,$blur,$blur); if ($index == -1) { $index = $image2->colorAllocate($blur,$blur,$blu +r); } $image2->setPixel($w,$h,$index); } } open(JPEG,">blur.jpg") || die $!; print JPEG $image2->jpeg; close(JPEG); } sub sharpen { my $image = GD::Image->newFromJpeg("image.jpg"); my ($width,$height) = $image->getBounds(); my $image2 = GD::Image->new($width,$height); for my $w (2..$width-1){ for my $h (2..$height-1){ my $index = $image->getPixel($w,$h); my (@rgb) = $image->rgb($index); my $sharpen = 5*($image->rgb($image->getPixel($w,$h)))[0]- ($image->rgb($image->getPixel($w-1,$h)))[0]- ($image->rgb($image->getPixel($w,$h-1)))[0]- ($image->rgb($image->getPixel($w+1,$h)))[0]- ($image->rgb($image->getPixel($w,$h+1)))[0]; $sharpen = sprintf("%.f",$sharpen); $sharpen = $sharpen > 255 ? 255 : ($sharpen < 0 ? 0 : $sharpen ); $index = $image2->colorExact($sharpen,$sharpen,$sharpen); if ($index == -1) { $index = $image2->colorAllocate($sharpen,$sharpe +n,$sharpen); } $image2->setPixel($w,$h,$index); } } open(JPEG,">sharpen.jpg") || die $!; print JPEG $image2->jpeg; close(JPEG); } sub emboss { my $factor = shift; $factor ||= 0; my $image = GD::Image->newFromJpeg("image.jpg"); my ($width,$height) = $image->getBounds(); my $image2 = GD::Image->new($width,$height); for my $w (2..$width-1){ for my $h (2..$height-1){ my $index = $image->getPixel($w,$h); my (@rgb) = $image->rgb($index); my $emboss = (-$image->rgb($image->getPixel($w-1,$h-1)))[0]+ ($image->rgb($image->getPixel($w+1,$h+1)))[0]+$factor; $emboss = sprintf("%.f",$emboss); $emboss = $emboss > 255 ? 255 : ($emboss < 0 ? 0 : $emboss); $index = $image2->colorExact($emboss,$emboss,$emboss); if ($index == -1) { $index = $image2->colorAllocate($emboss,$emboss, +$emboss); } $image2->setPixel($w,$h,$index); } } open(JPEG,">emboss.jpg") || die $!; print JPEG $image2->jpeg; close(JPEG); } sub invert { my $image = GD::Image->newFromJpeg("image.jpg"); my ($width,$height) = $image->getBounds(); my $image2 = GD::Image->new($width,$height); for my $w (1..$width){ for my $h (1..$height){ my $index = $image->getPixel($w,$h); my (@rgb) = $image->rgb($index); for(@rgb) { $_ = 255-$_ } $index = $image2->colorExact(@rgb); if ($index == -1) { $index = $image2->colorAllocate(@rgb); } $image2->setPixel($w,$h,$index); } } open(JPEG,">invert.jpg") || die $!; print JPEG $image2->jpeg; close(JPEG); } sub level { my $level; if (@_) { $level = shift; } $level ||= 0; my $image = GD::Image->newFromJpeg("image.jpg"); my ($width,$height) = $image->getBounds(); my $image2 = GD::Image->new($width,$height); for my $w (1..$width){ for my $h (1..$height){ my $index = $image->getPixel($w,$h); my (@rgb) = $image->rgb($index); for(@rgb) { $_ = $_+$level > 255 ? 255 : ($_+$level < 0 ? 0 : $_+$le +vel); } $index = $image2->colorExact(@rgb); if ($index == -1) { $index = $image2->colorAllocate(@rgb); } $image2->setPixel($w,$h,$index); } } open(JPEG,">level.jpg") || die $!; print JPEG $image2->jpeg; close(JPEG); } blur; sharpen; emboss; #Factor can be passed as argument invert; level; #Level can be passed as argument
Altho this code isn't all that clean and fast, it does the job.
Sample Output is here.

Greetz
Beatnik
... Quidquid perl dictum sit, altum viditur.

In reply to GD effects on B&W Images by Beatnik

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.