Merge pull request #5283 from radarhere/context_managers

Added context managers to documentation
This commit is contained in:
Hugo van Kemenade 2021-02-26 20:17:18 +02:00 committed by GitHub
commit a3f34e71ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 53 deletions

View File

@ -22,7 +22,7 @@ Windows).
.. code-block:: python
from PIL import Image
im = Image.open("hopper.jpg")
with Image.open("hopper.jpg") as im:
im.rotate(45).show()
Create thumbnails
@ -40,7 +40,7 @@ current directory preserving aspect ratios with 128x128 max resolution.
for infile in glob.glob("*.jpg"):
file, ext = os.path.splitext(infile)
im = Image.open(infile)
with Image.open(infile) as im:
im.thumbnail(size)
im.save(file + ".thumbnail", "JPEG")
@ -145,7 +145,7 @@ This crops the input image with the provided coordinates:
from PIL import Image
im = Image.open("hopper.jpg")
with Image.open("hopper.jpg") as im:
# The crop method from the Image module takes four coordinates as input.
# The right can also be represented as (left+width)
@ -167,7 +167,7 @@ This blurs the input image using a filter from the ``ImageFilter`` module:
from PIL import Image, ImageFilter
im = Image.open("hopper.jpg")
with Image.open("hopper.jpg") as im:
# Blur the input image using the filter ImageFilter.BLUR
im_blurred = im.filter(filter=ImageFilter.BLUR)
@ -181,7 +181,7 @@ This helps to get the bands of the input image:
from PIL import Image
im = Image.open("hopper.jpg")
with Image.open("hopper.jpg") as im:
print(im.getbands()) # Returns ('R', 'G', 'B')
.. automethod:: PIL.Image.Image.getbbox
@ -192,7 +192,7 @@ This helps to get the bounding box coordinates of the input image:
from PIL import Image
im = Image.open("hopper.jpg")
with Image.open("hopper.jpg") as im:
print(im.getbbox())
# Returns four coordinates in the format (left, upper, right, lower)
@ -222,7 +222,7 @@ This resizes the given image from ``(width, height)`` to ``(width/2, height/2)``
from PIL import Image
im = Image.open("hopper.jpg")
with Image.open("hopper.jpg") as im:
# Provide the target width and height of the image
(width, height) = (im.width // 2, im.height // 2)
@ -236,7 +236,7 @@ This rotates the input image by ``theta`` degrees counter clockwise:
from PIL import Image
im = Image.open("hopper.jpg")
with Image.open("hopper.jpg") as im:
# Rotate the image by 60 degrees counter clockwise
theta = 60
@ -260,7 +260,7 @@ This flips the input image by using the :data:`FLIP_LEFT_RIGHT` method.
from PIL import Image
im = Image.open("hopper.jpg")
with Image.open("hopper.jpg") as im:
# Flip the image from left to right
im_flipped = im.transpose(method=Image.FLIP_LEFT_RIGHT)

View File

@ -81,7 +81,7 @@ Example: Draw Partial Opacity Text
from PIL import Image, ImageDraw, ImageFont
# get an image
base = Image.open("Pillow/Tests/images/hopper.png").convert("RGBA")
with Image.open("Pillow/Tests/images/hopper.png").convert("RGBA") as base:
# make a blank image for the text, initialized to transparent text color
txt = Image.new("RGBA", base.size, (255,255,255,0))

View File

@ -15,8 +15,8 @@ Example: Using the :py:mod:`~PIL.ImageMath` module
from PIL import Image, ImageMath
im1 = Image.open("image1.jpg")
im2 = Image.open("image2.jpg")
with Image.open("image1.jpg") as im1:
with Image.open("image2.jpg") as im2:
out = ImageMath.eval("convert(min(a, b), 'L')", a=im1, b=im2)
out.save("result.png")

View File

@ -63,7 +63,7 @@ Take your test image, and make a really simple harness.
::
from PIL import Image
im = Image.open(path)
with Image.open(path) as im:
im.load()
- Run this through valgrind, but note that python triggers some issues