title: ImageMagick White to Transparent Background While Maintaining White Object
body: <p>I am using PHP's <a href="http://php.net/manual/en/book.imagick.php" rel="nofollow noreferrer">ImageMagick</a> to turn the white background of an image, transparent. I pass the image URL to this PHP script and it returns the image.</p>
  
  <pre><code>&lt;?php
  
  # grab the remote image url
  $imgUrl = $_GET['img'];
  
  # create new ImageMagick object
  $im = new Imagick($imgUrl);
  
  # remove extra white space
  $im-&gt;clipImage(0);
  
  # convert white background to transparent
  $im-&gt;paintTransparentImage($im-&gt;getImageBackgroundColor(), 0, 3000);
  
  # resize image --- passing 0 as width invokes proportional scaling
  $im-&gt;resizeImage(0, 200, Imagick::FILTER_LANCZOS, 1);
  
  # set resulting image format as png
  $im-&gt;setImageFormat('png');
  
  # set header type as PNG image
  header('Content-Type: image/png');
  
  # output the new image
  echo $im-&gt;getImageBlob();
  </code></pre>
  
  <p>These transformations work pretty well. However, if I have an image with a white object, it doesn't work very well as a result of passing a fuzz value to <a href="http://php.net/manual/en/imagick.painttransparentimage.php" rel="nofollow noreferrer">paintTransparentImage()</a>; which is how I am cleaning up jagged edges.</p>
  
  <p><strong>Here's an example of the result, note the white sofa:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/75ubJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/75ubJ.png" alt="enter image description here"></a></p>
  
  <p>If I don't pass a fuzz value, then I get a proper cut, but i'm left with messy edges:</p>
  
  <p><a href="https://i.stack.imgur.com/ThaRj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ThaRj.png" alt="enter image description here"></a></p>
  
  <p>I have attempted to use <a href="http://php.net/manual/en/imagick.resizeimage.php" rel="nofollow noreferrer">resizeImage()</a> in an effort to achieve what's called 'spatial anti-aliasing' (blow up the image really big -> use <a href="http://php.net/manual/en/imagick.painttransparentimage.php" rel="nofollow noreferrer">paintTransparentImage()</a> -> shrink image back down), but I didn't notice any significant changes.</p>
  
  <p>Is there something I can do to better handle these really white images? I've played about with <a href="http://php.net/manual/en/imagick.trimimage.php" rel="nofollow noreferrer">trimImage()</a> and <a href="http://php.net/manual/en/imagick.edgeimage.php" rel="nofollow noreferrer">edgeImage()</a>, but I'm not able to get the results i'm after.</p>
  
  <p>Worst case scenario, (though not ideal), is there a means of identifying if an image contains some percentage of a particular colour? IE. if image contains >90% white pixels then I could run <a href="http://php.net/manual/en/imagick.painttransparentimage.php" rel="nofollow noreferrer">paintTransparentImage()</a> with a fuzz value of 0 instead of 3000 which would at least give me a proper cut.</p>
  
  <p>Thanks.</p>