如何在 Python 中从计算机上的目录加载图像

ads*_*ton 3 image loading-image python-2.7

您好,我是 python 的新手,我想知道如何将计算机目录中的图像加载到 python 变量中。我在磁盘上的文件夹中有一组图像,我想循环显示这些图像。

小智 10

您可以使用 PIL(Python 图像库)http://www.pythonware.com/products/pil/加载图像。然后你可以制作一个脚本来从目录中读取图像并将它们加载到 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 = "/path/to/your/images/"

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

for img in imgs:
    # you can show every image
    img.show()
Run Code Online (Sandbox Code Playgroud)