mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-27 17:54:32 +03:00
Merge pull request #5283 from radarhere/context_managers
Added context managers to documentation
This commit is contained in:
commit
a3f34e71ed
|
@ -22,8 +22,8 @@ Windows).
|
||||||
.. 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:
|
||||||
im.rotate(45).show()
|
im.rotate(45).show()
|
||||||
|
|
||||||
Create thumbnails
|
Create thumbnails
|
||||||
^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^
|
||||||
|
@ -40,9 +40,9 @@ current directory preserving aspect ratios with 128x128 max resolution.
|
||||||
|
|
||||||
for infile in glob.glob("*.jpg"):
|
for infile in glob.glob("*.jpg"):
|
||||||
file, ext = os.path.splitext(infile)
|
file, ext = os.path.splitext(infile)
|
||||||
im = Image.open(infile)
|
with Image.open(infile) as im:
|
||||||
im.thumbnail(size)
|
im.thumbnail(size)
|
||||||
im.save(file + ".thumbnail", "JPEG")
|
im.save(file + ".thumbnail", "JPEG")
|
||||||
|
|
||||||
Functions
|
Functions
|
||||||
---------
|
---------
|
||||||
|
@ -145,15 +145,15 @@ This crops the input image with the provided coordinates:
|
||||||
|
|
||||||
from PIL import Image
|
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 crop method from the Image module takes four coordinates as input.
|
||||||
# The right can also be represented as (left+width)
|
# The right can also be represented as (left+width)
|
||||||
# and lower can be represented as (upper+height).
|
# and lower can be represented as (upper+height).
|
||||||
(left, upper, right, lower) = (20, 20, 100, 100)
|
(left, upper, right, lower) = (20, 20, 100, 100)
|
||||||
|
|
||||||
# Here the image "im" is cropped and assigned to new variable im_crop
|
# Here the image "im" is cropped and assigned to new variable im_crop
|
||||||
im_crop = im.crop((left, upper, right, lower))
|
im_crop = im.crop((left, upper, right, lower))
|
||||||
|
|
||||||
|
|
||||||
.. automethod:: PIL.Image.Image.draft
|
.. automethod:: PIL.Image.Image.draft
|
||||||
|
@ -167,10 +167,10 @@ This blurs the input image using a filter from the ``ImageFilter`` module:
|
||||||
|
|
||||||
from PIL import Image, ImageFilter
|
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
|
# Blur the input image using the filter ImageFilter.BLUR
|
||||||
im_blurred = im.filter(filter=ImageFilter.BLUR)
|
im_blurred = im.filter(filter=ImageFilter.BLUR)
|
||||||
|
|
||||||
.. automethod:: PIL.Image.Image.frombytes
|
.. automethod:: PIL.Image.Image.frombytes
|
||||||
.. automethod:: PIL.Image.Image.getbands
|
.. automethod:: PIL.Image.Image.getbands
|
||||||
|
@ -181,8 +181,8 @@ This helps to get the bands of the input image:
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
im = Image.open("hopper.jpg")
|
with Image.open("hopper.jpg") as im:
|
||||||
print(im.getbands()) # Returns ('R', 'G', 'B')
|
print(im.getbands()) # Returns ('R', 'G', 'B')
|
||||||
|
|
||||||
.. automethod:: PIL.Image.Image.getbbox
|
.. automethod:: PIL.Image.Image.getbbox
|
||||||
|
|
||||||
|
@ -192,9 +192,9 @@ This helps to get the bounding box coordinates of the input image:
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
im = Image.open("hopper.jpg")
|
with Image.open("hopper.jpg") as im:
|
||||||
print(im.getbbox())
|
print(im.getbbox())
|
||||||
# Returns four coordinates in the format (left, upper, right, lower)
|
# Returns four coordinates in the format (left, upper, right, lower)
|
||||||
|
|
||||||
.. automethod:: PIL.Image.Image.getchannel
|
.. automethod:: PIL.Image.Image.getchannel
|
||||||
.. automethod:: PIL.Image.Image.getcolors
|
.. automethod:: PIL.Image.Image.getcolors
|
||||||
|
@ -222,11 +222,11 @@ This resizes the given image from ``(width, height)`` to ``(width/2, height/2)``
|
||||||
|
|
||||||
from PIL import Image
|
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
|
# Provide the target width and height of the image
|
||||||
(width, height) = (im.width // 2, im.height // 2)
|
(width, height) = (im.width // 2, im.height // 2)
|
||||||
im_resized = im.resize((width, height))
|
im_resized = im.resize((width, height))
|
||||||
|
|
||||||
.. automethod:: PIL.Image.Image.rotate
|
.. automethod:: PIL.Image.Image.rotate
|
||||||
|
|
||||||
|
@ -236,12 +236,12 @@ This rotates the input image by ``theta`` degrees counter clockwise:
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
im = Image.open("hopper.jpg")
|
with Image.open("hopper.jpg") as im:
|
||||||
|
|
||||||
# Rotate the image by 60 degrees counter clockwise
|
# Rotate the image by 60 degrees counter clockwise
|
||||||
theta = 60
|
theta = 60
|
||||||
# Angle is in degrees counter clockwise
|
# Angle is in degrees counter clockwise
|
||||||
im_rotated = im.rotate(angle=theta)
|
im_rotated = im.rotate(angle=theta)
|
||||||
|
|
||||||
.. automethod:: PIL.Image.Image.save
|
.. automethod:: PIL.Image.Image.save
|
||||||
.. automethod:: PIL.Image.Image.seek
|
.. automethod:: PIL.Image.Image.seek
|
||||||
|
@ -260,12 +260,12 @@ This flips the input image by using the :data:`FLIP_LEFT_RIGHT` method.
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
im = Image.open("hopper.jpg")
|
with Image.open("hopper.jpg") as im:
|
||||||
|
|
||||||
# Flip the image from left to right
|
# Flip the image from left to right
|
||||||
im_flipped = im.transpose(method=Image.FLIP_LEFT_RIGHT)
|
im_flipped = im.transpose(method=Image.FLIP_LEFT_RIGHT)
|
||||||
# To flip the image from top to bottom,
|
# To flip the image from top to bottom,
|
||||||
# use the method "Image.FLIP_TOP_BOTTOM"
|
# use the method "Image.FLIP_TOP_BOTTOM"
|
||||||
|
|
||||||
|
|
||||||
.. automethod:: PIL.Image.Image.verify
|
.. automethod:: PIL.Image.Image.verify
|
||||||
|
|
|
@ -81,24 +81,24 @@ Example: Draw Partial Opacity Text
|
||||||
|
|
||||||
from PIL import Image, ImageDraw, ImageFont
|
from PIL import Image, ImageDraw, ImageFont
|
||||||
# get an image
|
# 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
|
# make a blank image for the text, initialized to transparent text color
|
||||||
txt = Image.new("RGBA", base.size, (255,255,255,0))
|
txt = Image.new("RGBA", base.size, (255,255,255,0))
|
||||||
|
|
||||||
# get a font
|
# get a font
|
||||||
fnt = ImageFont.truetype("Pillow/Tests/fonts/FreeMono.ttf", 40)
|
fnt = ImageFont.truetype("Pillow/Tests/fonts/FreeMono.ttf", 40)
|
||||||
# get a drawing context
|
# get a drawing context
|
||||||
d = ImageDraw.Draw(txt)
|
d = ImageDraw.Draw(txt)
|
||||||
|
|
||||||
# draw text, half opacity
|
# draw text, half opacity
|
||||||
d.text((10,10), "Hello", font=fnt, fill=(255,255,255,128))
|
d.text((10,10), "Hello", font=fnt, fill=(255,255,255,128))
|
||||||
# draw text, full opacity
|
# draw text, full opacity
|
||||||
d.text((10,60), "World", font=fnt, fill=(255,255,255,255))
|
d.text((10,60), "World", font=fnt, fill=(255,255,255,255))
|
||||||
|
|
||||||
out = Image.alpha_composite(base, txt)
|
out = Image.alpha_composite(base, txt)
|
||||||
|
|
||||||
out.show()
|
out.show()
|
||||||
|
|
||||||
Example: Draw Multiline Text
|
Example: Draw Multiline Text
|
||||||
----------------------------
|
----------------------------
|
||||||
|
|
|
@ -15,11 +15,11 @@ Example: Using the :py:mod:`~PIL.ImageMath` module
|
||||||
|
|
||||||
from PIL import Image, ImageMath
|
from PIL import Image, ImageMath
|
||||||
|
|
||||||
im1 = Image.open("image1.jpg")
|
with Image.open("image1.jpg") as im1:
|
||||||
im2 = Image.open("image2.jpg")
|
with Image.open("image2.jpg") as im2:
|
||||||
|
|
||||||
out = ImageMath.eval("convert(min(a, b), 'L')", a=im1, b=im2)
|
out = ImageMath.eval("convert(min(a, b), 'L')", a=im1, b=im2)
|
||||||
out.save("result.png")
|
out.save("result.png")
|
||||||
|
|
||||||
.. py:function:: eval(expression, environment)
|
.. py:function:: eval(expression, environment)
|
||||||
|
|
||||||
|
|
|
@ -63,8 +63,8 @@ Take your test image, and make a really simple harness.
|
||||||
::
|
::
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
im = Image.open(path)
|
with Image.open(path) as im:
|
||||||
im.load()
|
im.load()
|
||||||
|
|
||||||
- Run this through valgrind, but note that python triggers some issues
|
- Run this through valgrind, but note that python triggers some issues
|
||||||
on its own, so you're looking for items within the Pillow hierarchy
|
on its own, so you're looking for items within the Pillow hierarchy
|
||||||
|
|
Loading…
Reference in New Issue
Block a user