mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-10 08:12:33 +03:00
Use context manager when opening images [ci skip]
This commit is contained in:
parent
dd8b0de666
commit
04f7c75466
|
@ -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,14 +20,14 @@ 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)
|
||||||
draw.line((0, im.size[1], im.size[0], 0), fill=128)
|
draw.line((0, im.size[1], im.size[0], 0), fill=128)
|
||||||
|
|
||||||
# write to stdout
|
# write to stdout
|
||||||
im.save(sys.stdout, "PNG")
|
im.save(sys.stdout, "PNG")
|
||||||
|
|
||||||
|
|
||||||
Concepts
|
Concepts
|
||||||
|
|
|
@ -14,12 +14,11 @@ 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)
|
index += 1
|
||||||
index += 1
|
|
||||||
|
|
||||||
The :py:class:`~PIL.ImageSequence.Iterator` class
|
The :py:class:`~PIL.ImageSequence.Iterator` class
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
|
|
|
@ -17,8 +17,8 @@ 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)
|
||||||
print (px[4,4])
|
print (px[4,4])
|
||||||
|
|
|
@ -18,8 +18,8 @@ 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)
|
||||||
print (px[4,4])
|
print (px[4,4])
|
||||||
|
|
Loading…
Reference in New Issue
Block a user