Skip to content

Too many open files error #1237

@indirectlylit

Description

@indirectlylit

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))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions