mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-02-04 21:50:54 +03:00
added context manager support
This commit is contained in:
parent
1c4dc75fab
commit
196a48b4fd
19
PIL/Image.py
19
PIL/Image.py
|
@ -497,6 +497,25 @@ class Image:
|
||||||
|
|
||||||
_makeself = _new # compatibility
|
_makeself = _new # compatibility
|
||||||
|
|
||||||
|
# with compatibility
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
def __exit__(self, *args):
|
||||||
|
self.close()
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
""" Close the file pointer, if possible. Destroy the image core.
|
||||||
|
This releases memory, and the image will be unusable afterward
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
self.fp.close()
|
||||||
|
except Exception as msg:
|
||||||
|
if Image.DEBUG:
|
||||||
|
print ("Error closing: %s" %msg)
|
||||||
|
|
||||||
|
self.im = None
|
||||||
|
|
||||||
|
|
||||||
def _copy(self):
|
def _copy(self):
|
||||||
self.load()
|
self.load()
|
||||||
self.im = self.im.copy()
|
self.im = self.im.copy()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user