Python:使用PIL库读取图像时的AttributeError

2024-04-20 08:48:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用PIL库处理一组图像。导入图像没有问题,但是当我试图从列表中访问第一个图像的大小信息时,我收到了一个错误。功能:

def loadImages(path):

    image_path = listdir(path)
    image_list = []
    for img in image_path:
        image = Image.open(path + img)
        image_list.append(image)

    return image_list

path = 'path/to/images'

images = loadImages(path)
N = len(images)
print("Number of images:", N)

w,h= Image.open(images[0]).size

以及整个错误列表:

^{pr2}$

我正在研究: 枕头5.0.0 python 3.6.4


Tags: path图像image功能信息列表imgpil
1条回答
网友
1楼 · 发布于 2024-04-20 08:48:27

看起来你要打开图像两次。在loadImage中一次,然后在以后尝试打开Image.open返回的内容(已经是图像):

w,h = Image.open(images[0]).size

因为images[0] = Image.open(path + listdir(path)[0]),您可以有效地使用上面的行执行以下操作:

^{pr2}$

你应该能够做到:

w, h = images[0].size

相关问题 更多 >