title: Python: PIL replace a single RGBA color
body: <p>I have already taken a look at this question: <a href="https://stackoverflow.com/questions/1616767/pil-best-way-to-replace-color">SO question</a> and seem to have implemented a very similar technique for replacing a single color including the alpha values:</p>
  
  <pre><code>c = Image.open(f)
  c = c.convert("RGBA")
  w, h = c.size
  cnt = 0
  for px in c.getdata():
      c.putpixel((int(cnt % w), int(cnt / w)), (255, 0, 0, px[3]))
      cnt += 1                                                                                                   
  </code></pre>
  
  <p>However, this is very slow. I found <a href="http://mail.python.org/pipermail/image-sig/2002-December/002092.html" rel="noreferrer">this recipe</a> out on the interwebs, but have not had success using it thus far.</p>
  
  <p>What I am trying to do is take various PNG images that consist of a single color, white.  Each pixel is 100% white with various alpha values, including alpha = 0.  What I want to do is basically colorize the image with a new set color, for instance #ff0000&lt;00-ff>.  SO my starting and resulting images would look like this where the left side is my starting image and the right is my ending image (NOTE: background has been changed to a light gray so you can see it since it is actually transparent and you wouldn't be able to see the dots on the left.)</p>
  
  <p><img src="https://i.stack.imgur.com/aVyKO.png" alt="alt text"></p>
  
  <p>Any better way to do this?</p>