-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Improve lifecycle documentation #5772
Comments
https://pillow.readthedocs.io/en/stable/reference/open_files.html#image-lifecycle
Pixel data is required for As for improving the documentation, what do you think of #5773? |
Thank you for the clarification, suggested change helps! What you say also implies that a context manager is not required for the new with Image.open("some.jpg") as img:
new_img = img.convert("L")
# Closes the context on `img` but does not affect `new_img`.
print(new_img.format, new_img.mode) # Safe to do with `img` also? is safe to do, correct? |
That is safe to do with If you use So the following code will work, because from PIL import Image
with Image.open("some.jpg") as img:
if img.fp is not None:
print("img.fp is not None inside the context manager")
new_img = img.convert("L")
if img.fp is None:
print("img.fp is None outside the context manager")
img.save("out.png") But this code will not work, because from PIL import Image
with Image.open("some.jpg") as img:
pass
img.save("out.png") |
@radarhere, great, I think that too should be mentioned in PR #5773 👍🏼 |
I've added a variation on the passing code I wrote to the PR. The failing code is already explained in https://pillow.readthedocs.io/en/stable/reference/open_files.html#complications |
The Image lifecycle documentation states that
However, functions like Image.convert()
It’s unclear whether it’s necessary to call
close()
on such newly created objects or not, i.e. what’s the lifecycle here? Is the underlying file shared amongst these two objects?The text was updated successfully, but these errors were encountered: