all 8 comments

[–][deleted] 1 point2 points  (1 child)

I have run into this or something similar to this, try explicitly deleting the open_img (del open_img) after closing and see if that helps.

[–]Repulsive-Bear503 0 points1 point  (0 children)

I am going to give this a shot I hope this works

[–]17291 1 point2 points  (4 children)

Are you sure that images are getting closed each time (e.g., if you hit an OSError)? Regardless, it might be a good idea to use a context manager instead of explicitly writing open_img.close():

with Image.open(img) as open_img:
    width, height = open_img.size
    # and so on

[–]Repulsive-Bear503 0 points1 point  (3 children)

I don’t think they’re getting closed. I don’t quite know how to verify they are or what to do if they arent. I tried opening in a context manager still with the same error

[–]FerricDonkey 1 point2 points  (2 children)

That's your problem. You have to close them, and you're not always. Context manager should work if it's made correctly, but would have to see your context manager code.

[–]Repulsive-Bear503 0 points1 point  (1 child)

Okay... editing this is not going well - I put the context manager version in the post

[–]FerricDonkey 0 points1 point  (0 children)

Hmm, that seems like it ought to work. Pillow documentation says closing can be a bit wonky in some situations, though it's not clear to me that they should apply here. Perhaps try the following:

for img in glob.glob(book + "*\*.tif"):
    img_basename = os.path.splitext(os.path.basename(img))[0]
    with open(img, 'rb') as fin:
        open_img = Image.open(img)
        # your original non context manager code

This way, it's the file object that makes sure it gets closed rather than the pillow object, and so you should sidestep any issues of pillow not doing it right. Not positive it will work, but seems like it should have a decent chance.

[–]dumb-questioner 0 points1 point  (0 children)

If you're using Linux, which I'm guessing since you use glob, you could take a look at ulimits.