mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-05-29 02:03:25 +03:00
Merge pull request #4435 from radarhere/close_images
Close exclusively opened images
This commit is contained in:
commit
0f7ed2d35f
|
@ -74,7 +74,8 @@ Convert files to JPEG
|
||||||
outfile = f + ".jpg"
|
outfile = f + ".jpg"
|
||||||
if infile != outfile:
|
if infile != outfile:
|
||||||
try:
|
try:
|
||||||
Image.open(infile).save(outfile)
|
with Image.open(infile) as im:
|
||||||
|
im.save(outfile)
|
||||||
except IOError:
|
except IOError:
|
||||||
print("cannot convert", infile)
|
print("cannot convert", infile)
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ Example: Draw a gray cross over an image
|
||||||
|
|
||||||
from PIL import Image, ImageDraw
|
from PIL import Image, ImageDraw
|
||||||
|
|
||||||
im = Image.open("hopper.jpg")
|
with Image.open("hopper.jpg") as im:
|
||||||
|
|
||||||
draw = ImageDraw.Draw(im)
|
draw = ImageDraw.Draw(im)
|
||||||
draw.line((0, 0) + im.size, fill=128)
|
draw.line((0, 0) + im.size, fill=128)
|
||||||
|
|
|
@ -14,8 +14,7 @@ Extracting frames from an animation
|
||||||
|
|
||||||
from PIL import Image, ImageSequence
|
from PIL import Image, ImageSequence
|
||||||
|
|
||||||
im = Image.open("animation.fli")
|
with Image.open("animation.fli") as im:
|
||||||
|
|
||||||
index = 1
|
index = 1
|
||||||
for frame in ImageSequence.Iterator(im):
|
for frame in ImageSequence.Iterator(im):
|
||||||
frame.save("frame%d.png" % index)
|
frame.save("frame%d.png" % index)
|
||||||
|
|
|
@ -17,7 +17,7 @@ changes it.
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
im = Image.open('hopper.jpg')
|
with Image.open('hopper.jpg') as im:
|
||||||
px = im.load()
|
px = im.load()
|
||||||
print (px[4,4])
|
print (px[4,4])
|
||||||
px[4,4] = (0,0,0)
|
px[4,4] = (0,0,0)
|
||||||
|
|
|
@ -18,7 +18,7 @@ The following script loads an image, accesses one pixel from it, then changes it
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
im = Image.open('hopper.jpg')
|
with Image.open('hopper.jpg') as im:
|
||||||
px = im.load()
|
px = im.load()
|
||||||
print (px[4,4])
|
print (px[4,4])
|
||||||
px[4,4] = (0,0,0)
|
px[4,4] = (0,0,0)
|
||||||
|
|
|
@ -141,8 +141,8 @@ def Ghostscript(tile, size, fp, scale=1):
|
||||||
startupinfo = subprocess.STARTUPINFO()
|
startupinfo = subprocess.STARTUPINFO()
|
||||||
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
|
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
|
||||||
subprocess.check_call(command, startupinfo=startupinfo)
|
subprocess.check_call(command, startupinfo=startupinfo)
|
||||||
im = Image.open(outfile)
|
out_im = Image.open(outfile)
|
||||||
im.load()
|
out_im.load()
|
||||||
finally:
|
finally:
|
||||||
try:
|
try:
|
||||||
os.unlink(outfile)
|
os.unlink(outfile)
|
||||||
|
@ -151,7 +151,9 @@ def Ghostscript(tile, size, fp, scale=1):
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return im.im.copy()
|
im = out_im.im.copy()
|
||||||
|
out_im.close()
|
||||||
|
return im
|
||||||
|
|
||||||
|
|
||||||
class PSFile:
|
class PSFile:
|
||||||
|
|
|
@ -370,7 +370,7 @@ if __name__ == "__main__":
|
||||||
for size in imf.info["sizes"]:
|
for size in imf.info["sizes"]:
|
||||||
imf.size = size
|
imf.size = size
|
||||||
imf.save("out-%s-%s-%s.png" % size)
|
imf.save("out-%s-%s-%s.png" % size)
|
||||||
im = Image.open(sys.argv[1])
|
with Image.open(sys.argv[1]) as im:
|
||||||
im.save("out.png")
|
im.save("out.png")
|
||||||
if sys.platform == "windows":
|
if sys.platform == "windows":
|
||||||
os.startfile("out.png")
|
os.startfile("out.png")
|
||||||
|
|
|
@ -71,7 +71,10 @@ class ImageFont:
|
||||||
def _load_pilfont(self, filename):
|
def _load_pilfont(self, filename):
|
||||||
|
|
||||||
with open(filename, "rb") as fp:
|
with open(filename, "rb") as fp:
|
||||||
|
image = None
|
||||||
for ext in (".png", ".gif", ".pbm"):
|
for ext in (".png", ".gif", ".pbm"):
|
||||||
|
if image:
|
||||||
|
image.close()
|
||||||
try:
|
try:
|
||||||
fullname = os.path.splitext(filename)[0] + ext
|
fullname = os.path.splitext(filename)[0] + ext
|
||||||
image = Image.open(fullname)
|
image = Image.open(fullname)
|
||||||
|
@ -81,11 +84,14 @@ class ImageFont:
|
||||||
if image and image.mode in ("1", "L"):
|
if image and image.mode in ("1", "L"):
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
if image:
|
||||||
|
image.close()
|
||||||
raise OSError("cannot find glyph data file")
|
raise OSError("cannot find glyph data file")
|
||||||
|
|
||||||
self.file = fullname
|
self.file = fullname
|
||||||
|
|
||||||
return self._load_pilfont_data(fp, image)
|
self._load_pilfont_data(fp, image)
|
||||||
|
image.close()
|
||||||
|
|
||||||
def _load_pilfont_data(self, file, image):
|
def _load_pilfont_data(self, file, image):
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,9 @@ def grab(bbox=None, include_layered_windows=False, all_screens=False):
|
||||||
im.load()
|
im.load()
|
||||||
os.unlink(filepath)
|
os.unlink(filepath)
|
||||||
if bbox:
|
if bbox:
|
||||||
im = im.crop(bbox)
|
im_cropped = im.crop(bbox)
|
||||||
|
im.close()
|
||||||
|
return im_cropped
|
||||||
else:
|
else:
|
||||||
offset, size, data = Image.core.grabscreen(include_layered_windows, all_screens)
|
offset, size, data = Image.core.grabscreen(include_layered_windows, all_screens)
|
||||||
im = Image.frombytes(
|
im = Image.frombytes(
|
||||||
|
|
|
@ -203,4 +203,5 @@ if __name__ == "__main__":
|
||||||
print("Syntax: python ImageShow.py imagefile [title]")
|
print("Syntax: python ImageShow.py imagefile [title]")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
print(show(Image.open(sys.argv[1]), *sys.argv[2:]))
|
with Image.open(sys.argv[1]) as im:
|
||||||
|
print(show(im, *sys.argv[2:]))
|
||||||
|
|
|
@ -158,7 +158,7 @@ class IptcImageFile(ImageFile.ImageFile):
|
||||||
o.close()
|
o.close()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
_im = Image.open(outfile)
|
with Image.open(outfile) as _im:
|
||||||
_im.load()
|
_im.load()
|
||||||
self.im = _im.im
|
self.im = _im.im
|
||||||
finally:
|
finally:
|
||||||
|
|
|
@ -448,7 +448,7 @@ class JpegImageFile(ImageFile.ImageFile):
|
||||||
raise ValueError("Invalid Filename")
|
raise ValueError("Invalid Filename")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
_im = Image.open(path)
|
with Image.open(path) as _im:
|
||||||
_im.load()
|
_im.load()
|
||||||
self.im = _im.im
|
self.im = _im.im
|
||||||
finally:
|
finally:
|
||||||
|
|
|
@ -304,7 +304,7 @@ if __name__ == "__main__":
|
||||||
print("input image must be in Spider format")
|
print("input image must be in Spider format")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
im = Image.open(filename)
|
with Image.open(filename) as im:
|
||||||
print("image: " + str(im))
|
print("image: " + str(im))
|
||||||
print("format: " + str(im.format))
|
print("format: " + str(im.format))
|
||||||
print("size: " + str(im.size))
|
print("size: " + str(im.size))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user