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

后端 未结 5 940
醉话见心
醉话见心 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条回答
  •  猫巷女王i
    2020-12-08 04:32

    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."

提交回复
热议问题