title: Python mask image pixels from a set of values
body: <p>Provided an image with labels (the value of a pixel corresponds to its label), and the list of labels that are accepted, I am trying to create a "mask" image with <code>255</code> value if the pixels label is accepted, <code>0</code> otherwise.</p>
  
  <p>I know that this is a slow approach as it iterates over the image at python-speed (but it demonstrates the idea well):</p>
  
  <pre><code>mask = numpy.zeros(labels.shape[:2], dtype = "uint8")
  
  for i in xrange(mask.shape[0]):
      for j in xrange(mask.shape[1]):
          if labels[i][j] in accepted:
              mask[i][j] = 255
  </code></pre>
  
  <p>I know that it is much faster to use python slicing and masking, but I do not know how to compose a complicated condition. I still get a tremendous speed-up when I mask the pixels one-by-one accepted label, like so:</p>
  
  <pre><code>for value in accepted:
      mask[labels == value] = 255
  </code></pre>
  
  <p>Can I somehow make a one-liner doing what I want? My python knowledge is rusty (read: almost no python in the last few years), so while I tried composing this using some examples I found, this is the closest I got:</p>
  
  <pre><code>mask[(labels in accepted).all()] = 255
  </code></pre>
  
  <p>And in this case I get the following error: <code>ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()</code></p>
  
  <p>I've looked at similar SO questions (e.g.<a href="https://stackoverflow.com/questions/13461153/how-can-i-iterate-over-image-pixels-in-a-faster-manner-in-python">here</a> or <a href="https://stackoverflow.com/questions/26445153/iterations-through-pixels-in-an-image-are-terribly-slow-with-python-opencv">here</a> and more) but they all seem to cover the cases where either the values are from a range or lower/higher than a threshold (&lt;10) , or where the image slice to change is continuous.</p>
  
  <p>Any suggestion on how check "is value among accepted values" would be great.</p>