3

I got an error with this code:

import PIL.ImageOps
inverted_image = PIL.ImageOps.invert(image)

The traceback is

Traceback (most recent call last):
  File "filter.py", line 32, in <module>
    inverted_image = PIL.ImageOps.invert(denoise)
  File "/usr/lib/python3/dist-packages/PIL/ImageOps.py", line 367, in invert
    return _lut(image, lut)
  File "/usr/lib/python3/dist-packages/PIL/ImageOps.py", line 48, in _lut
    if image.mode == "P":
AttributeError: 'numpy.ndarray' object has no attribute 'mode'

How can I fix it?

1
  • 2
    The line of code you post does not match your traceback. Please edit your question to include your actual code. Commented May 20, 2017 at 19:53

1 Answer 1

4

The Exception is raised if you try to do ImageOps.invert on a numpy.ndarray:

>>> import numpy as np
>>> from PIL import Image
>>> from PIL import ImageOps
>>> ImageOps.invert(np.ones((100, 100, 3), dtype=np.uint8))
AttributeError: 'numpy.ndarray' object has no attribute 'mode'

To solve this issue you need to convert it to an Image:

>>> img = Image.fromarray(np.ones((100, 100, 3), dtype=np.uint8))  # RGB image
>>> ImageOps.invert(img)
# works
Sign up to request clarification or add additional context in comments.

1 Comment

TypeError: fromarray() got an unexpected keyword argument 'dtype'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.