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

后端 未结 5 931
醉话见心
醉话见心 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:18

    In case someone is inverting a CMYK image, the current implementations of PIL and Pillow don't seem to support this and throw an error. You can, however, easily circumvent this problem by inverting your image's individual bands using this handy function (essentially an extension of Greg Sadetsky's post above):

    def CMYKInvert(img) :
        return Image.merge(img.mode, [ImageOps.invert(b.convert('L')) for b in img.split()])
    

提交回复
热议问题