python-3.x - 图像无法使用 Image.open(fnames) 打开,并且代码有 "' 列表'对象没有属性 'read'“错误

标签 python-3.x image subdirectory

我有一些子文件夹(s1,s2,s3,...)。我想打开路径中“Image”文件夹的子文件夹中的图像:
“E:/Image/”并显示它们。但图片打不开。

import os
from PIL import Image
root = "E:/Image/" 
path = os.path.join(root,"")
for r in os.walk(path):
     for file in r:
        fnames = glob.glob(f'{root}{r}/{file}')
        img1 = Image.open(fnames)
        img1.show()  

我的代码有这个错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~\Anaconda3\lib\site-packages\PIL\Image.py in open(fp, mode)
   2546     try:
-> 2547         fp.seek(0)
   2548     except (AttributeError, io.UnsupportedOperation):

AttributeError: 'list' object has no attribute 'seek'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
<ipython-input-5-e9dafc96965e> in <module>()
      9         fnames = glob.glob(f'{root}{r}/{file}')
     10 
---> 11         img1 = Image.open(fnames)
     12        
     13         img1.show()

~\Anaconda3\lib\site-packages\PIL\Image.py in open(fp, mode)
   2547         fp.seek(0)
   2548     except (AttributeError, io.UnsupportedOperation):
-> 2549         fp = io.BytesIO(fp.read())
   2550         exclusive_fp = True
   2551 

AttributeError: 'list' object has no attribute 'read'

最佳答案

您收到异常是因为您将列表而不是文件名传递给 Image.open

但是,我看到的主要问题与 os.walk 的错误使用有关。和 glob.glob 。 我建议在使用您不知道的功能之前仔细阅读文档。

澄清:

  • os.walk 不会返回 path 中的文件名称,而是递归地返回目录 (dirpath)、列表子目录 (dirnames) 和目录中的文件列表 (filenames)。您只需对 filenames 中的每个 name 执行 os.path.join(dirpath, name) 即可获取所有文件名。
  • glob.glob 返回与给定通配符匹配的文件列表。从您的代码中,我想您希望 f'{root}{r}/{file}' 是系统上文件的名称,因此无需使用使您的生活复杂化glob.glob

一些注意事项:

  • 由于您的 root 已经有一个尾随 /,因此对 os.path.join(root,"") 的调用不会执行任何操作;
  • 如果您想以编程方式创建文件路径,请不要手动添加路径分隔符,而是尝试使用os.path.join(或者如果需要的话os.sep) 尽可能多(这有助于保持跨系统兼容性)。作为示例而不是这样做

    root = "E:/Image/" 
    r = 'folder'
    file = 'test.txt'
    fname = f'{root}{r}/{file}'
    

    使用

    root = "E:/Image" 
    r = 'folder'
    file = 'test.txt'
    fname = os.path.join(root, r, file)
    

代码

现在是“正确”的代码。由于我不知道您的目录结构、用户案例等,因此代码现在可能可以按您想要的方式工作,但我希望您可以根据您的需要进行调整。

import os
from PIL import Image

root = "E:/Image/" 
for dirpath, _, filenames in os.walk(path):
    # we don't care about ``dirnames``
    for name in filenames:
        fname = op.path.join(dirpath, name)
        img1 = Image.open(fname)
        img1.show()  

关于python-3.x - 图像无法使用 Image.open(fnames) 打开,并且代码有 "' 列表'对象没有属性 'read'“错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50084406/

相关文章:

python - 如何在 "correctly"复制一个 types.SimpleNamespace 对象?

python - Python 中枚举的枚举?

php - mysql View 增加 2 而不是 1

python - 是否可以在 Python 图像库 (PIL) 中屏蔽图像?

angularjs - 使用 Angular JS 加载图像后更新 DIV 元素

python - 复杂的排序,使用 cmp 函数很容易完成,但我如何为 Python 3 做计划?

python - 用于浏览网站的合适 Python 模块

java - 使用java无法访问文件夹中的文件

C# 搜索子目录(不是文件)

php - 使用 htaccess 和 php 创建动态子目录