title: How do I find and remove white specks from an image using SciPy/NumPy?
body: <p>I have a series of images which serve as my raw data which I am trying to prepare for publication. These images have a series of white specks randomly throughout which I would like to replace with the average of some surrounding pixels.</p>
  
  <p>I cannot post images, but the following code should produce a PNG that approximates the issue that I'm trying to correct:</p>
  
  <pre><code>import numpy as np
  from scipy.misc import imsave
  
  random_array = np.random.random_sample((512,512))
  
  random_array[random_array &lt; 0.999] *= 0.25
  
  imsave('white_specs.png', random_array)
  </code></pre>
  
  <p>While this should produce an image with a similar <em>distribution</em> of the specks present in my raw data, my images <em>do not</em> have specks uniform in intensity, and some of the specks are more than a single pixel in size (though none of them are more than 2). Additionally, there are spots on my image that I <em>do not</em> want to alter that were <em>intentionally</em> saturated during data acquisition for the purpose of clarity when presented: these spots are approximately 10 pixels in diameter.</p>
  
  <p>In principle, I could write something to look for pixels whose value exceeds a certain threshold then check them against the average of their nearest neighbors. However, I assume what I'm ultimately trying to achieve is not an uncommon action in image processing, and I very much suspect that there is some SciPy functionality that will do this without having to reinvent the wheel. My issue is that I am not familiar enough with the formal aspects/vocabulary of image processing to really know what I should be looking for. Can someone point me in the right direction?</p>