简体   繁体   中英

'numpy.ndarray' object has no attribute 'mode'

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM