mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-11-04 01:47:47 +03:00 
			
		
		
		
	Merge pull request #5283 from radarhere/context_managers
Added context managers to documentation
This commit is contained in:
		
						commit
						a3f34e71ed
					
				| 
						 | 
				
			
			@ -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)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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))
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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")
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user