首页 > 解决方案 > problem in showing all images from folder python

问题描述

I want to show all images from a folder through python but got this error -

File "<tokenize>", line 18
    imagesList = listdir(path) IndentationError: unindent does not match any outer indentation level

code:

from os import listdir
from PIL import Image as PImage

def loadImages(path):

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

return loadedImages

path = "CATS_DOGS/train/CAT/"
 imgs = loadImages(path)

for img in imgs:
img.show()

标签: python

解决方案


正如错误所说,这只是一个缩进问题。

应该是这样的:

from os import listdir
from PIL import Image as PImage

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

path = "CATS_DOGS/train/CAT/"
imgs = loadImages(path)

for img in imgs:
   img.show()

推荐阅读