我发现这个代码有一个错误:
import PIL.ImageOps
inverted_image = PIL.ImageOps.invert(image)
回溯是
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'
我怎样才能修好它?
最佳答案
如果您尝试对aImageOps.invert
执行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'
要解决此问题,您需要将其转换为
Image
:>>> img = Image.fromarray(np.ones((100, 100, 3), dtype=np.uint8)) # RGB image
>>> ImageOps.invert(img)
# works