Closed mansuf closed 2 years ago
Could we have copies of the images? It would be helpful to remove as much ambiguity as possible from your example.
You changed the title to "Memory leaks in PIL.PngImagePlugin.PngImageFile.close". So you believe that this problem can be replicated without saving as PDFs and without using your Pillow modification?
If you're concerned about memory when saving PDFs, you might also be interested in this method that uses less memory. If that works, it would also remove the need for your Pillow modification.
Could we have copies of the images? It would be helpful to remove as much ambiguity as possible from your example.
Unfortunately, i cannot send you the images in this issue publicly, because of copyright reason. Is there a way that i can send images privately ?
You changed the title to "Memory leaks in PIL.PngImagePlugin.PngImageFile.close". So you believe that this problem can be replicated without saving as PDFs and without using your Pillow modification?
I assume that variable im_sequence
is PIL.PngImagePlugin.PngImageFile
. And no, the problem only happened in low-level function _save()
in `PdfImagePlugin.py.
If you're concerned about memory when saving PDFs, you might also be interested in https://github.com/python-pillow/Pillow/issues/4067#issuecomment-531517647 If that works, it would also remove the need for your Pillow modification.
I have tried this before. In my case, converting thousand of images to PDF file in single run can cause slow performance. I have looked at Pillow module and found that https://github.com/python-pillow/Pillow/blob/9.2.0/src/PIL/PdfImagePlugin.py#L112 is causing slow, because the PDF plugin is trying to load the whole contents of PDF file. So modifying Pillow module is best option so far.
I cannot send you the original images that i used for tests, but this should do.
Source: https://commons.wikimedia.org/wiki/File:Lattinaichnusa.png
After testing further with every extensions available (PIL.Image.EXTENSION
). This is the result i found.
Not affected
Failed
Affected
Thanks for the test image. Testing opening that image 16 times with your modification, I'm able to replicate the problem. The change in behaviour comes from https://github.com/python-pillow/Pillow/pull/6096/commits/bb9338e34d705501b87876ebe6a8212b88b96acb
I've created PR #6456 to resolve this. If you could test it and confirm, that would be helpful.
Really confident that the PR resolve the problem, thank you very much 👍
What did you do?
I'm converting 16 PNG images (with resolution 3000x4000) to PDF file and since Pillow didn't close the images after converting, it can cause huge memory. So i modified it's code from https://github.com/python-pillow/Pillow/blob/9.2.0/src/PIL/PdfImagePlugin.py#L218-L227, it looks like this
What did you expect to happen?
The memory stay low during converting
What actually happened?
Usually it worked for Pillow 9.0.1 and the memory stays between 20-60 MB. But after installing version 9.2.0, 9.1.0, and 9.1.1 it increased up to 700+ MB
Take a look at this footages
Pillow 9.0.1
https://user-images.githubusercontent.com/43638783/179394112-0aa70a49-b7e1-42e5-8dda-0306adc6d180.mp4Pillow 9.1.0
https://user-images.githubusercontent.com/43638783/179394412-abc0d24f-f09a-414e-8b5c-f542c558ba75.mp4Pillow 9.1.1
https://user-images.githubusercontent.com/43638783/179394649-84cdaaa4-85a8-41c5-ad15-c2c0198fe045.mp4Pillow 9.2.0
https://user-images.githubusercontent.com/43638783/179395101-d9a77ae8-dc18-4e23-a7f0-b14064f85f2b.mp4What are your OS, Python and Pillow versions?