python pil invert image color

Solutions on MaxInterview for python pil invert image color by the best coders in the world

showing results for - "python pil invert image color"
Dario
22 Oct 2020
1from PIL import Image
2import PIL.ImageOps    
3
4image = Image.open('your_image.png')
5
6inverted_image = PIL.ImageOps.invert(image)
7
8inverted_image.save('new_name.png')
9
Drake
26 Jan 2018
1#If the image is RGBA transparent this will fail... This should work though:
2
3from PIL import Image
4import PIL.ImageOps    
5
6image = Image.open('your_image.png')
7if image.mode == 'RGBA':
8    r,g,b,a = image.split()
9    rgb_image = Image.merge('RGB', (r,g,b))
10
11    inverted_image = PIL.ImageOps.invert(rgb_image)
12
13    r2,g2,b2 = inverted_image.split()
14
15    final_transparent_image = Image.merge('RGBA', (r2,g2,b2,a))
16
17    final_transparent_image.save('new_file.png')
18
19else:
20    inverted_image = PIL.ImageOps.invert(image)
21    inverted_image.save('new_name.png')
22