How to invert colors of image with PIL (Python-Imaging)?

bialix picture bialix · Mar 23, 2010 · Viewed 75.4k times · Source

I need to convert series of images drawn as white on black background letters to images where white and black are inverted (as negative). How can I achieve this using PIL?

Answer

Gary Kerr picture Gary Kerr · Mar 23, 2010

Try the following from the docs: http://effbot.org/imagingbook/imageops.htm

from PIL import Image
import PIL.ImageOps    

image = Image.open('your_image.png')

inverted_image = PIL.ImageOps.invert(image)

inverted_image.save('new_name.png')

Note: "The ImageOps module contains a number of 'ready-made' image processing operations. This module is somewhat experimental, and most operators only work on L and RGB images."