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
image.putdata(pixels)
image.save('new.bmp')

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 = Image.open(filename)
pixels = list(image.getdata())

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

image.putdata(new_pixels)
image.save('new.bmp')
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.