title: Inverting pixels of an RGB image in Python
body: <p>I'm trying to invert the pixels of an RGB image. That is, simply subtracting the intensity value of each channel (red, green, blue) of each pixel from <code>255</code>. </p>
  
  <p>I have the following so far:</p>
  
  <pre><code>from PIL import Image
  
  im = Image.open('xyz.png')
  rgb_im = im.convert('RGB')
  width, height = im.size
  
  output_im = Image.new('RGB', (width,height))
  
  for w in range(width):
      for h in range(height):
          r,g,b = rgb_im.getpixel((w,h))
          output_r = 255 - r
          output_g = 255 - g
          output_b = 255 - b
          output_im[w,h] = (output_r, output_g, output_b)
  </code></pre>
  
  <p>When I run the above script, I get the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "image_inverse.py", line 31, in &lt;module&gt;
      output_im[w,h] = (output_r, output_g, output_b)
    File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 528, in __getattr__
      raise AttributeError(name)
  AttributeError: __setitem__
  </code></pre>
  
  <p>How can I solve this issue?</p>
  
  <p>Thanks.</p>