“打开的文件太多"在枕头中打开和加载图像时出现错误 [英] "Too many open files" error when opening and loading images in Pillow

查看:65
本文介绍了“打开的文件太多"在枕头中打开和加载图像时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行以下代码时:

KEEP=[]
for file in glob.glob("./KEEP/thing*.[tT][iI][fF]"):
    m = pattern.search(file)
    filename=m.group(1)
    keep=Image.open(file)
    keep.load()
    KEEP.append(keep)
    KEEP_NAMES.append(filename)
    keep.close()

超过一千个文件,我收到错误消息:

over more than a thousand files, I get the error message:

Traceback (most recent call last):
  File "/hom/yannis/texmf/python/remove-harakat.py", line 123, in <module>
  File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 2237, in open
IOError: [Errno 24] Too many open files: './KEEP/thing1118_26.TIF'

我不明白为什么会这样,因为我正在加载(然后关闭)所有文件,为什么还要保持打开状态? 除了减少文件数量(对我来说这不是一个选择)之外,是否有解决此问题的方法?在内存中读取了它们的内容后,有什么方法可以关闭它们?

I don't understand why this is happening, since I'm load()ing and then close()ing all files, why should they remain open? Is there a solution to this problem, other than reducing the number of files (which is not an option for me)? Some way to close them after their contents have been read in memory?

推荐答案

这可能是Image.load方法的错误-请参见#1237 .

This may be a bug with the Image.load method - see Pillow issue #1144. I ran into the same too many open files error - see #1237.

我的解决方法是将图像加载到临时对象中,进行复制,然后显式关闭该临时对象.对于您的代码,它看起来像这样:

My work-around was to load the image into a temp object, make a copy, and then explicitly close the temp. For your code it would look something like this:

KEEP=[]
for file in glob.glob("./KEEP/thing*.[tT][iI][fF]"):
    m = pattern.search(file)
    filename = m.group(1)
    temp = Image.open(file)
    keep = temp.copy()
    KEEP.append(keep)
    KEEP_NAMES.append(filename)
    temp.close()

这篇关于“打开的文件太多"在枕头中打开和加载图像时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆