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

后端 未结 5 930
醉话见心
醉话见心 2020-12-08 04:00

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?

5条回答
  •  一向
    一向 (楼主)
    2020-12-08 04:24

    For anyone working with an image in "1" mode (i.e., 1-bit pixels, black and white, stored with one pixel per byte -- see docs), you need to convert it into "L" mode before calling PIL.ImageOps.invert.

    Thus:

    im = im.convert('L')
    im = ImageOps.invert(im)
    im = im.convert('1')
    

提交回复
热议问题