【问题标题】:Load all images from a folder using PIL使用 PIL 从文件夹中加载所有图像
【发布时间】:2019-02-13 00:31:06
【问题描述】:
import glob
from PIL import Image 


marked = glob.iglob("D:/Users/username/Desktop/cells/Marked")

img = Image.open(marked)
img.show()

我正在尝试使用标记的文件夹中包含的图像数据集创建神经网络。当我运行代码时出现错误:

 File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PIL/Image.py", line 2547, in open
    fp.seek(0)
AttributeError: 'generator' object has no attribute 'seek'

我不明白这个错误是什么意思,它似乎被误导了?

【问题讨论】:

标签: python python-imaging-library


【解决方案1】:

您需要在路径末尾指定通配符并进行迭代:

images = []
for f in glob.iglob("D:/Users/username/Desktop/cells/Marked/*"):
    images.append(np.asarray(Image.open(f)))

images = np.array(images)

【讨论】:

  • 投反对票的人,你能给我反馈以改进这个答案吗?
【解决方案2】:

参见this answer,它使用 PIL.Image 和 glob 查找文件夹中的所有图像并将它们加载到数组中。

from PIL import Image
import glob
image_list = []
for filename in glob.glob('yourpath/*.gif'): #assuming gif
    im=Image.open(filename)
    image_list.append(im)

【讨论】:

  • 与@coldspeed 已经接受的答案相比,这有什么优势?
  • 只是指出存在一个同样有效的现有答案(也包括导入语句)。
猜你喜欢
  • 2016-05-28
  • 2011-12-09
  • 1970-01-01
  • 2017-12-03
  • 1970-01-01
  • 2011-07-07
  • 2015-07-25
  • 2014-02-26
  • 2012-06-24
相关资源
最近更新 更多