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.
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'
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))
You may be interested in #1144
Duplicate of #1144
Workaround works.
Workaround working fine for me, with over 3000 frames.
Most helpful comment
Workaround works.