gpt4 book ai didi

python - 使用 PIL `Image.fromarray` 创建图像会导致 AttributeError : 'list' object has no attribute '__array_interface__'

转载 作者:行者123 更新时间:2023-12-02 01:40:58 59 4
gpt4 key购买 nike

我想显示 NumPy 数组中的图像,但出现此错误:

Traceback (most recent call last):
File "E:/wittos/python/SVM/witti svm/arraytoimage.py", line 14, in <module>
image = Image.fromarray(arry)
File "C:\Users\MOHAMED-WITTI-ADOU\AppData\Local\Programs\Python\Python35\lib\site-packages\PIL\Image.py", line 2483, in fromarray
arr = obj.__array_interface__
AttributeError: 'list' object has no attribute '__array_interface__'

我希望你能帮我解决这个错误。

import numpy as np
from PIL import Image

# Create a NumPy array
arry = np.array([3,3])
arry= [[25,25,25],[0,0,0],[0,0,0]]

# Create a PIL image from the NumPy array
image = Image.fromarray(arry)

# Save the image
image.save('image.jpg')

最佳答案

您创建 numpy 数组的方式是错误的。您应该将其创建为:

arry = np.array([[25,25,25],[0,0,0],[0,0,0]])

然后就可以了。因为,您正在覆盖用普通数组创建的空 numpy 数组。

import numpy as np
from PIL import Image

# Create a NumPy array
arry = np.array([[25,25,25],[0,0,0],[0,0,0]])

# Create a PIL image from the NumPy array
image = Image.fromarray(arry.astype('uint8'))

# Save the image
image.save('image.jpg')

这会起作用。

关于python - 使用 PIL `Image.fromarray` 创建图像会导致 AttributeError : 'list' object has no attribute '__array_interface__' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53969450/

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