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

2023-12-07

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


您可以使用 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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何用Python从计算机上的目录加载图像 的相关文章

随机推荐