Altho this code isn't all that clean and fast, it does the job.#!/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
In reply to GD effects on B&W Images by Beatnik
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |