从桌面路径显示图像

时间:2018-08-06 17:28:07

标签: python image desktop reader

#!/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]目录名称无效:   'C:\ Users \ Aidan \ Desktop \ APDR_PDhh_epi5new.bmp'

以上是错误。

我的桌面上有一个名为“ APDR_PDhh_epi5new.bmp”的位图文件,但出现错误。我在做什么错了?

1 个答案:

答案 0 :(得分:1)

您正在listdir上呼叫path,但是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()