PHP Imagick outline/contour and sketch

谁都会走 提交于 2019-12-23 04:26:49

问题


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

  1. Get image with (more/less) clear, consistent background (for example: white, red or transparent)
  2. Remove background if it is set
  3. Add outline (specific color)
  4. Remove image inside

Result: http://i57.tinypic.com/2wg91qx.png

Plan #2 Sketch

  1. Get image with (more/less) clear, consistent background (for example: white, red or transparent)
  2. Remove background if it is set
  3. Add sketch effect
  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!