Pillow: `Too many open files` error

Created on 21 May 2015  路  4Comments  路  Source: python-pillow/Pillow

Issue

While generating thumbnails of a few hundred large image files, I'm getting a IOError: [Errno 24] Too many open files. My understanding is that the Image.load method should "close the file associated with the image", so it's not clear to me why this happens. That said, this could certainly be user error.

The problem looks similar to this question on stackoverflow.

System

  • OSX 10.9.5
  • Python 2.7.9
  • Pillow 2.8.1

    Reproducing

Python 2.7.9 (default, Feb 10 2015, 03:29:19) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import glob
>>> names = glob.glob('*.JPG')
>>> len(names)
415
>>> thumbs = {}
>>> from PIL import Image
>>> for n in names:
...   thumbs[n] = Image.open(n)
...   thumbs[n].load()
...   thumbs[n].thumbnail((300, 300))
... 
<PixelAccess object at 0x108156110>
<PixelAccess object at 0x108156170>
<PixelAccess object at 0x108156190>
<PixelAccess object at 0x1081561b0>
<PixelAccess object at 0x1081561d0>
<PixelAccess object at 0x1081561f0>

_(244 similar lines omitted)_

<PixelAccess object at 0x10b7fd110>
<PixelAccess object at 0x10b7fd130>
<PixelAccess object at 0x10b7fd150>
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/Users/d/PythonEnvs/main/lib/python2.7/site-packages/PIL/Image.py", line 2248, in open
IOError: [Errno 24] Too many open files: 'DSC00271.JPG'

Workaround

I was able to work around this issue by creating a copy of the image object, similar to this:

for n in names:
    img = Image.open(n)
    thumbs[n] = img.copy()
    img.close()
    thumbs[n].thumbnail((300, 300))

Most helpful comment

Workaround works.

All 4 comments

You may be interested in #1144

Duplicate of #1144

Workaround works.

Workaround working fine for me, with over 3000 frames.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

naaaargle picture naaaargle  路  3Comments

anonymous530 picture anonymous530  路  3Comments

thinrhino picture thinrhino  路  3Comments

edowson picture edowson  路  3Comments

etc0de picture etc0de  路  4Comments