gpt4 book ai didi

python - 'numpy.ndarray' 对象没有属性 'mode'

转载 作者:行者123 更新时间:2023-11-28 22:27:20 24 4
gpt4 key购买 nike

我在这段代码中遇到错误:

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'

我该如何解决?

最佳答案

如果您尝试执行 ImageOps.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

关于python - 'numpy.ndarray' 对象没有属性 'mode',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44090179/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com