Add support for 1-bit images to ImageOps.invert()

This commit is contained in:
Dov Grobgeld 2022-02-07 15:24:36 +02:00 committed by GitHub
parent f018518e24
commit 9a78e37936
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -520,6 +520,11 @@ def invert(image):
:param image: The image to invert.
:return: An image.
"""
# Special case for 1-bit images using python translate() method
if image.mode == '1':
lut = bytearray(range(255,-1,-1))
return Image.frombuffer('1',image.size,image.tobytes().translate(lut))
lut = []
for i in range(256):
lut.append(255 - i)