-
Hello, I'm wondering how to invert lightness of an image while preserving colors, using Pillow? A demo of what I mean is Okular's "Invert Lightness" accessibility option. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
I think you could convert the image to HSV and then invert the V channel. |
Beta Was this translation helpful? Give feedback.
-
Here is the full example using HLS color space: import colorsys
from PIL import Image, ImageFilter
def invert_colors(r, g, b):
h, l, s = colorsys.rgb_to_hls(r, g, b)
l = 1.0 - l
return colorsys.hls_to_rgb(h, l, s)
invert_filter = ImageFilter.Color3DLUT.generate(17, invert_colors)
Image.open('257923775.png').filter(invert_filter).save('257923775.out.png') |
Beta Was this translation helpful? Give feedback.
-
def invlight(r, g, b):
# lightness = (max(r,g,b) + min(r,g,b)) / 2
# lightnessnew = 255 - lightness
# deltalightness = lightnessnew - lightness = 255 - 2 * lightness
dl = 255 - (max(r,g,b) + min(r,g,b))
return [r + dl, g + dl, b + dl] |
Beta Was this translation helpful? Give feedback.
Here is the full example using HLS color space: