I'm editing an image with PIL (Python Imaging Library). On each step (convert, rotate, resize ...) there are more images created. (An excerpt from the documentation: "Returns a copy of an image rotated the given number of degrees ...") So I want to release memory.
Do you know whether the following approach saves memory?
import PIL.Image
image = PIL.Image.open('Image.jpg')
garbage = image
image = image.convert('RGB')
del garbage
You don't need to make the temporary garbage
reference.
When the right-hand side of this statement is executed:
image = image.convert('RGB')
a new Python object is created.
By assigning it back to image
the old object that image
used to represent has its reference count reduced to zero, and is sent to the garbage collector.
However, not related to how Python works, I have seen PIL issues where because of genuine bugs memory leaks have formed. For instance here's a discussion of issues when using Draw text:
PIL Draw Text Memory Leak
I know that's a really old discussion, but I still see that come up sometimes when I use PIL!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With