Anyone ever used the PIL (Python Image Library)? If you have, is there anyway to convert a single pixel at a time in a bmp file? I'm trying to invert a black and white image to white and black. So:

import PIL
image = Image open(filename)
pixels = list(image.getdata())
#insert someway to invert the pixels

I think I also have to use a for pixel in pixels loop.

I know there are easier ways with the in-built functions but I have to do it this way.

Problem solved, I ended up Googling for an hour and got:

new_pixels = [ 255-pixel for pixel in pixels]

Thanks for letting us know. So your final code is something like this ...

# invert a black&white bitmap file image

from PIL import Image

filename = "old.bmp"

image =
pixels = list(image.getdata())

# process the pixels (invert)
new_pixels = [255-pixel for pixel in pixels]


Edited 5 Years Ago by vegaseat: n/a

This question has already been answered. Start a new discussion instead.