Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSError: [Errno 24] Too many open files for image.save for tiff images #5936

Closed
gooseillo opened this issue Jan 6, 2022 · 7 comments · Fixed by #5946
Closed

OSError: [Errno 24] Too many open files for image.save for tiff images #5936

gooseillo opened this issue Jan 6, 2022 · 7 comments · Fixed by #5946

Comments

@gooseillo
Copy link

gooseillo commented Jan 6, 2022

What did you do?
I am opening a large number of jpg and tiff (single page & multi page) images, resizing them and saving them in a new directory. I am running this code within VS code in jupyter notebook using PIL 9.0.0

    def change_image_res(rep_image, 
                        new_path,
                        max_image_thresh=49, 
                        percent_reduce_res = [0.1, 0.2, 0.3, 0.4, 0.5]):

    image_name = rep_image.name
    save_all = True if rep_image.suffix == '.tiff' else False

    new_path = Path(new_path)

    with open(rep_image, "rb") as f:
        image = Image.open(f)
    
        if (image.size[0]*image.size[1])/1000000 > max_image_thresh:
            for res in percent_reduce_res:
                fixed_height = int(int((float(image.size[1]))) - (res * int((float(image.size[1])))))
                height_percent = (fixed_height / float(image.size[1]))
                width_size = int((float(image.size[0]) * float(height_percent)))
                if fixed_height*width_size/1000000 < 50:
                    image = image.resize((width_size, fixed_height), PIL.Image.NEAREST)
                    image.save(f'{new_path}\{image_name}', dpi=(300,300), save_all=save_all)
                    break
        else:
            image.save(f'{new_path}\{image_name}', dpi=(300,300), save_all=save_all)

What did you expect to happen?
I expected the images to be opened, processed, saved and closed automatically because of the context manager.

What actually happened?

After opening over a thousand images I get the following error

OSError: [Errno 24] Too many open files

I also notice that the python.exe instance builds up around 5GB ram. Restarting the Kernel releases the memory

I have tried every thing possible on the internet like temp copy, deep copy, using close(), using load() etc.
I have also tried every thing in this issue

#2760

Screenshot 2022-01-07 002627

@radarhere
Copy link
Member

If you didn't have the context manager, I would suggest calling image.close - https://pillow.readthedocs.io/en/stable/releasenotes/7.0.0.html#image-del

But you do, so that makes things more interesting.

Is there any way for you to provide us with a self-contained example including images, so that we can run the code and see the error for ourselves?

@gooseillo
Copy link
Author

gooseillo commented Jan 7, 2022

self_contained_test.zip

I tried it without the context manager along with image.close but got the same error

In the folder which I have attached, I ran python change_img_res.py. After 1014 loops, it gives the following error

Screenshot 2022-01-07 110955

@radarhere
Copy link
Member

The zip you've attached is empty?

@gooseillo
Copy link
Author

gooseillo commented Jan 7, 2022

Apologies, I updated the attachment.

@radarhere
Copy link
Member

radarhere commented Jan 9, 2022

Thanks. I was able to replicate the error using your attachment - radarhere@031dae421260 / https://ci.appveyor.com/project/radarhere/pillow/builds/42132497/job/qnvpmk4ougrx4x6l

I've concluded that the problem is because of this

# libtiff closes the file descriptor, so pass in a dup.
try:
fp = hasattr(self.fp, "fileno") and os.dup(self.fp.fileno())

Perhaps libtiff doesn't always close the file pointer?

If I ensure that it is closed, then the CI job passes.

So I've created PR #5946 to resolve this.

@gooseillo
Copy link
Author

Thanks for this, the new change solves this problem.

@radarhere
Copy link
Member

Be aware that #7199 has been opened, using an alternative method to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants