The explanations in Image.load() and Image.close() methods are confusing
In Image.load():
This method will close the file associated with the image.
and in Image.close():
This function is only required to close images that have not had their file read and closed by the load() method.
I guess these explanations are derived from the following behaviours:
>>> from PIL import Image
>>> img = Image.open("01.jpg")
>>> img.fp
<_io.BufferedReader name='01.jpg'>
>>> img.fp.close() # success
>>> img = Image.open("01.jpg")
>>> img.fp
<_io.BufferedReader name='01.jpg'>
>>> px = img.load() # load() close fp??
>>> img.fp
>>> img.fp.close() # fail, as the documents claims so that.
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'close'
>>>
But these expressions is confusing, I think. At first, the "load" method does not always call the "close" method, I think:
>>> img = Image.open("01.jpg")
>>> img.resize((img.width // 4, img.height // 4)).save("01.eps")
>>> img = Image.open("01.eps")
>>> px = img.load() # load() close fp??
>>> img.fp
<_io.BufferedReader name='01.eps'>
>>> img.fp.close() # no, close operation will succeed.
Secondly, we might fear about when we should not call the "close" method.
Essentially, the documentation should only note that "if you want to release the resource immediately, you can call this method explicitly", I think. Could you simplify these explanations?
In the first place, I think this topic will be more clear for us with explanation by (open, load) pair. I guess:
-
open: It opens the file, and checks minimally, but doesn't read the file contents for interpreting as image data into memory.
-
load: It reads the file content, decodes it, and expands the image into memory. When finished decoding, the opened file (pointer) is no more needed, then library can close it.
Normally, this method is called implicitly, but if you do nothing except but 'open', then the library get lost the chance to closing file pointer, so in this case you should call the 'close' method explicitly.
Question: "you should call the 'close' method explicitly" phrase is truth? What happens when we forget to call 'close'?
I hope the documents answers these kind of question.
BTW. I'm not native English speaker. So, I am sorry if it is difficult to communicate.
This is probably the best explanation of what's happening:
https://github.com/python-pillow/Pillow/pull/2330#issuecomment-280877645
(there's more detail in the rest of the thread)
This is probably the best explanation of what's happening:
Thanks. Approximately as I imagined.
Simply, I'm just afraid that users will become uneasy when they reads these explanetions:
- Is there a case we should not call 'close'?
- Maybe the answer is NO. If this case exists, it is a bug.
- In what case should we always do call 'close'?
- In this case, what happens if we forget to call 'close'?
- After all, what should we do?
In the case of this topic, I guess it is only necessary to pay attention to lead the users good practice. The explanation that entered inside is confusing. Can we introduce enduser-friendly general rule? For example:
- Generally, if you run the code having long lifetime, calling 'close' always is good practice.
- But if target image is multiframe, take care of its timing to call 'close'.
Best practice is to call close, and any access after calling close is an error. This is similar to file access.
Best practice is to call close, and any access after calling close is an error. This is similar to file access.
Yes, of course.
Repeat: I guess it is only necessary to pay attention to lead the users good practice.
I don't mean "I don't understand it" but mean "the users might be confused". The explanations in 'load', 'close' section are too deep diving to inside for most of users, so my proposal is just to note those general rule.
We've added links in the documentation in https://github.com/python-pillow/Pillow/pull/3214, so closing this. Further documentation improvements always welcome.
TNX. It was what I wished at the end of the linked document.