python - 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()
解决方案
正如错误所说,这只是一个缩进问题。
应该是这样的:
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()
推荐阅读
- bash - 编写 shell 脚本以在目录中的多个文件上运行 Rscript
- python-3.x - 如何从 Python 中的 HTML 表格列中提取数据
- sql - PostgreSQL - 获取所有具有最小值最大值的行
- mysql - mysql 在插入带参数的值时出错
- c# - 如何在 C++ 和 C# 中跟踪函数的进度
- json - 使用“十一缓存资产”时无法遍历数组
- mysql - NodeJS Seqeulize 一对一关系在数据库级别不真实?
- r - 在 R 中使用字符串作为参数
- node.js - 我需要使用 mailgun 处理传入的邮件,这不起作用
- python - FastAPI:我也可以将 Depends() 用于 POST 中的参数吗?