从桌面路径显示图像

2024-09-30 06:31:37 发布

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

#!/usr/bin/python
from os import listdir
from PIL import Image as PImage

def loadImages(path):
    # return array of images

    imagesList = listdir(path)
    loadedImages = []
    for image in imagesList:
        img = PImage.open(path + image)
        loadedImages.append(img)

    return loadedImages

path = r"C:\Users\Aidan\Desktop\APDR_PDhh_epi5new.bmp"

# your images in an array
imgs = loadImages(path)

for img in imgs:
    # you can show every image
    img.show()

NotADirectoryError: [WinError 267] The directory name is invalid: 'C:\Users\Aidan\Desktop\APDR_PDhh_epi5new.bmp'

以上是错误。在

我有一个名为“APDR_PDhh_epi5”的位图文件新建.bmp“在我的桌面上,我遇到了一个错误。我做错什么了?在


Tags: pathinfromimageimportimgreturnarray
1条回答
网友
1楼 · 发布于 2024-09-30 06:31:37

您正在对path调用listdir,但是C:\Users\Aidan\Desktop\APDR_PDhh_epi5new.bmp不是一个目录。这是一个文件。请尝试为path提供一个目录。另外,您应该使用os.path.join来创建open的参数,而不是使用字符串连接。在

import os
from PIL import Image as PImage

def loadImages(path):
    # return array of images

    imagesList = os.listdir(path)
    loadedImages = []
    for image in imagesList:
        img = PImage.open(os.path.join(path,image))
        loadedImages.append(img)

    return loadedImages

path = r"C:\Users\Aidan\Desktop"

# your images in an array
imgs = loadImages(path)
print(imgs)
for img in imgs:
    # you can show every image
    img.show()

相关问题 更多 >

    热门问题