[Image-SIG] too many files open

Janto Dreijer jantod at gmail.com
Tue May 29 22:06:46 CEST 2007


Hi!

I have a suggestion that would hopefully allow slightly better lazy
loading of Images. Currently data is only read from disk when
operations are applied to an image. This works great when images are
few and large.

Unfortunately ImageFile.__init__ opens a handle to the file. On
systems with a maximum of ~255 handles per process (?) this is causing
some problems, throwing a "too many files open" exception. Reports of
this happening can be found through Google.

I'm suggesting adding the following 2 lines to the start of ImageFile.load:

        if self.fp is None and self.filename:
                self.fp = open(self.filename, "rb")

This will allow one to do the following and still use images as normal.
images = []
for i in range(10000):
    image = Image.open("bla.png")
    image.verify() # release file handle
    images.append(image)

Maybe I'm missing something obvious as I haven't tested this
exhaustively. It seemed to solve my problem, so I'm just throwing this
out there.

Regards
Janto


More information about the Image-SIG mailing list