How to load images from a directory on the computer in Python

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 20:54:06

问题


Hello I am New to python and I wanted to know how i can load images from a directory on the computer into python variable. I have a set of images in a folder on disk and I want to display these images in a loop.


回答1:


You can use PIL (Python Imaging Library) http://www.pythonware.com/products/pil/ to load images. Then you can make an script to read images from a directory and load them to python, something like this.

#!/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()



回答2:


You can use glob and imageio python packages to achieve the same. Below is code in python 3:

import glob
import imageio

for image_path in glob.glob("<your image directory path>\\*.png"):
    im = imageio.imread(image_path)
    print (im.shape)
    print (im.dtype)


来源:https://stackoverflow.com/questions/36774431/how-to-load-images-from-a-directory-on-the-computer-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!