title: How do you compare pixels?
body: <p>I am using the PIL to take an image with a black background and make a mask out of it. What I want the program to do is iterate through all the pixels in the image and if the pixel is black make it white and if it is any other color make it black, but I am not sure how to appropriately compare pixel values to determine what to do with the pixel.</p>
  
  <p>Here is my code so far which creates an all black image.</p>
  
  <pre><code>import os, sys
  import Image
  
  filename = "C:\Users\pdiffley\Dropbox\C++2\Code\Test\BallSpriteImage.bmp"
  height   = 50
  width    = 50
  
  
  im = Image.open(filename)
  im = im.load()
  
  i = 0
  j = 0
  while i&lt;height:
      while j&lt;width:
          if im[j,i] == (0,0,0):
              im[j,i] = (255,255,255)
          else:
              im[j,i] = (0,0,0) 
          j = j+1
      i = i+1
  mask = Image.new('RGB', (width, height))
  newfile = filename.partition('.')
  newfile = newfile[0] + "Mask.bmp"
  
  mask.save(newfile)
  </code></pre>
  
  <p>I believe the problem is in the if statement comparing the im[j,i] to the RGB value (0,0,0) which always evaluates to false. What is the correct way to compare the pixel?</p>