cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | # |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 3 | # Example of modifying all the pixels in an image (like -fx). |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4 | # |
anthony | 285167e | 2010-09-29 07:24:06 +0000 | [diff] [blame^] | 5 | # Currently this is slow as each pixel is being lokedup one pixel at a time. |
| 6 | # The better technique of extracting and modifing a whole row of pixels at |
| 7 | # a time has not been figured out, though perl functions have been provided |
| 8 | # for this. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9 | # |
| 10 | # Also access and controls for Area Re-sampling (EWA), beyond single pixel |
| 11 | # lookup (interpolated unscaled lookup), is also not available at this time. |
| 12 | # |
| 13 | # Anthony Thyssen 5 October 2007 |
| 14 | # |
| 15 | use strict; |
| 16 | use Image::Magick; |
| 17 | |
| 18 | # read original image |
| 19 | my $orig = Image::Magick->new(); |
| 20 | my $w = $orig->Read('rose:'); |
| 21 | warn("$w") if $w; |
| 22 | exit if $w =~ /^Exception/; |
| 23 | |
| 24 | |
| 25 | # make a clone of the image for modifications |
| 26 | my $dest = $orig->Clone(); |
| 27 | |
| 28 | # You could enlarge destination image here if you like. |
| 29 | # And it is posible to modify the existing image directly |
| 30 | # rather than modifying a clone as FX does. |
| 31 | |
| 32 | # Iterate over destination image... |
| 33 | my ($width, $height) = $dest->Get('width', 'height'); |
| 34 | |
| 35 | for( my $j = 0; $j < $height; $j++ ) { |
| 36 | for( my $i = 0; $i < $width; $i++ ) { |
| 37 | |
| 38 | # read original image color |
| 39 | my @pixel = $orig->GetPixel( x=>$i, y=>$j ); |
| 40 | |
| 41 | # modify the pixel values (as normalized floats) |
| 42 | $pixel[0] = $pixel[0]/2; # darken red |
| 43 | |
| 44 | # write pixel to destination |
| 45 | # (quantization and clipping happens here) |
| 46 | $dest->SetPixel(x=>$i,y=>$j,color=>\@pixel); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | # display the result (or you could save it) |
| 51 | $dest->Write('win:'); |
| 52 | $dest->Write('pixel_fx.gif'); |
| 53 | |