Monday, April 30, 2012

Perform Artistic SketchImage Effect on Image using ImageMagick in PHP

This is a really neat function, definitely something for those into image manipulation in the style of Gimp/Photoshop effects.  The parameters are a bit unusual, though, and should be explained.

The "Radius of the Gaussian" (first parameter) is the width of the brush being used with the Sketch effect.  The "Standard Deviation of the Gaussian" (second parameter) is how often and to what extent there are "white space" areas between brush strokes.  If the Standard Deviation is big, the white areas are fewer but longer and deeper, but small, the white areas are many and tiny.  Too big, and it'll be too noticable an effect.  Do not that the higher the Standard Deviation goes, the significantly greater amount of time it takes to do the processing, compared to the other parameters.  The third parameter is the easiest: the angle.  That's just the angle that the brush strokes are drawn at uniformly throughout the image.  At 0, it's left-to-right, and at 90, it's up-and-down.  Don't forget you can use a negative degree to set the angle for this effect.  Ideally, you're probably going to want something between 30 and 60 degrees, if you're particularly aiming for an "artistic" effect.

And now, a sample demonstration on a 600 x 450 resolution image :

<?php

        // Grab Image File Data
        // ---------------------------------------------
       
    $file_to_grab_with_location = "image_workshop_directory/test.bmp";
   
    $imagick_type = new Imagick();
   
        // Open File
        // ---------------------------------------------
   
    $file_handle_for_viewing_image_file = fopen($file_to_grab_with_location, 'a+');

    $imagick_type->readImageFile($file_handle_for_viewing_image_file);

        // Perform Function
        // ---------------------------------------------

            // Gaussian Radius of 60 pixels,
            //   Gaussian Standard Deviation of 7 pixels,
            //   and Angle of -35 degrees.
   
    $imagick_type->sketchImage(60, 7, -35);
   
        // Filename
        // ---------------------------------------------
       
    $file_to_save_with_location = "image_workshop_directory/test_result.bmp";

        // Save File
        // ---------------------------------------------
   
    $file_handle_for_saving_image_file = fopen($file_to_save_with_location, 'a+');

    $imagick_type->writeImageFile($file_handle_for_saving_image_file);
       
?>

An example using the effect on an ocean picture from google image search...



Note:  This function is perfect for creating a "Rain Effect" to any picture.  Line it up to +/- 35 degrees or higher to get the optimal use out of it that way.

Note:  You'd think that this would make the drawing go into a black-gray-white scale, because it's a "sketch", but no, it actually retains the color.  That gives you the option to do more effects to it, like Posterize or OilImage.  Or, just convert it to a black and white scale to get a "true" Sketch effect.

Official Function Page: http://www.php.net/manual/en/imagick.sketchimage.php

// Note: All code appearing on the PHP Revolution blog by the blog owner is released under the Hacktivismo Enhanced-Source Software License Agreement (HESSLA), unless otherwise noted.  http://www.hacktivismo.com/about/hessla.php

No comments:

Post a Comment