问题
I have a problem with getting fine contour of the image.
How to do it with PHP Imagick?
Input image: Imagick wizard
Plan #1 Outline
- Get image with (more/less) clear, consistent background (for example: white, red or transparent)
- Remove background if it is set
- Add outline (specific color)
- Remove image inside
Result: http://i57.tinypic.com/2wg91qx.png
Plan #2 Sketch
- Get image with (more/less) clear, consistent background (for example: white, red or transparent)
- Remove background if it is set
- Add sketch effect
- Remove image inside
Result: http://i60.tinypic.com/az9vr5.png
PS: borders and/or shadows didnt' work for me well
回答1:
There are many ways to outline a picture. Here's one of them that does more or less what you wanted. Note that wizard's picture requires some extra processing. First background isn't fully white (it has some #FEFEFE or alike pixels). Also what is more troubling the upper part of the desk is filled with pure white. So you can either use white pixels after blurring as background (my way) or try to flood fill from the corner with matteFloodfillImage()
. However this may leave space between desk legs not transparent.
function drawImage(Imagick $i)
{
$i->setImageFormat("png");
header("Content-Type: image/" . $i->getImageFormat());
echo $i;
exit;
}
$o = new Imagick('wizard.png');
$o->setImageBackgroundColor('white'); // handle tranparent images
$o = $o->flattenImages(); // flatten after setting background
$o->blurImage(5, 30);
$o->whiteThresholdImage( "#F8F8F8" );
$o->blackThresholdImage( "#FFFFFF" );
$o->edgeImage(5);
$o->negateImage(false);
$o->paintTransparentImage($o->getImagePixelColor(0, 0), 0, 2000);
$o->colorizeImage("red", 1);
drawImage($o);
Sketching is a little more complex and I would recommend further reading on IM capabilities http://www.imagemagick.org/Usage/photos/#color-in
来源:https://stackoverflow.com/questions/26057570/php-imagick-outline-contour-and-sketch