In Python while unsing the PIL pillow library. When I open an image and call load on it do it still need to close it?

1 week ago 8
ARTICLE AD BOX

EDIT: It seems like i was confusing closing of the image file with .close() destroying the image data. Memory shouldn't be the problem rather properly closing the file.

When opening a image in Python using the PIL pillow library, assuming the image does not contain multiple frames with (e.g. a simple "jpeg") and then call .load() on it:

from PIL import Image image = Image.open("some path") image.load()

Can I just delete/reassign the reference to it and let the garbage collector take care of it instead of calling .close() on it? As instead of doing .close() could i just do:

image = "some other thing" # or del image

And the memory will still be released via the garbage collector?

The documentation states:

Proposed file handling

Image.Image.load() should close the image file, unless there are multiple frames.

Image.Image.seek() should never close the image file.

Users of the library should use a context manager or call Image.Image.close() on any image opened with a filename or Path object to ensure that the underlying file is closed.

EDIT: It says .load() should close the file as long as it contains one frame. So I assume that that means that .close() does not need to be called if no context manager is used, the image is only one frame and .load() has been called.

But most examples i have seen call .close() anyways. Thats why I am confused.

Read Entire Article