Merge pull request #5866 from radarhere/code

This commit is contained in:
Hugo van Kemenade 2021-12-04 09:43:08 +02:00 committed by GitHub
commit 2d769b0842
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 67 additions and 48 deletions

View File

@ -760,7 +760,7 @@ The :py:meth:`~PIL.Image.open` method sets the following attributes:
A convenience method, :py:meth:`~PIL.SpiderImagePlugin.SpiderImageFile.convert2byte`, A convenience method, :py:meth:`~PIL.SpiderImagePlugin.SpiderImageFile.convert2byte`,
is provided for converting floating point data to byte data (mode ``L``):: is provided for converting floating point data to byte data (mode ``L``)::
im = Image.open('image001.spi').convert2byte() im = Image.open("image001.spi").convert2byte()
Writing files in SPIDER format Writing files in SPIDER format
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -1201,6 +1201,7 @@ dpi. To load it at another resolution:
.. code-block:: python .. code-block:: python
from PIL import Image from PIL import Image
with Image.open("drawing.wmf") as im: with Image.open("drawing.wmf") as im:
im.load(dpi=144) im.load(dpi=144)
@ -1212,15 +1213,19 @@ To add other read or write support, use
from PIL import Image from PIL import Image
from PIL import WmfImagePlugin from PIL import WmfImagePlugin
class WmfHandler: class WmfHandler:
def open(self, im): def open(self, im):
... ...
def load(self, im): def load(self, im):
... ...
return image return image
def save(self, im, fp, filename): def save(self, im, fp, filename):
... ...
wmf_handler = WmfHandler() wmf_handler = WmfHandler()
WmfImagePlugin.register_handler(wmf_handler) WmfImagePlugin.register_handler(wmf_handler)

View File

@ -176,7 +176,8 @@ Rolling an image
xsize, ysize = image.size xsize, ysize = image.size
delta = delta % xsize delta = delta % xsize
if delta == 0: return image if delta == 0:
return image
part1 = image.crop((0, 0, delta, ysize)) part1 = image.crop((0, 0, delta, ysize))
part2 = image.crop((delta, 0, xsize, ysize)) part2 = image.crop((delta, 0, xsize, ysize))
@ -264,6 +265,7 @@ Converting between modes
:: ::
from PIL import Image from PIL import Image
with Image.open("hopper.ppm") as im: with Image.open("hopper.ppm") as im:
im = im.convert("L") im = im.convert("L")
@ -462,6 +464,7 @@ Reading from an open file
:: ::
from PIL import Image from PIL import Image
with open("hopper.ppm", "rb") as fp: with open("hopper.ppm", "rb") as fp:
im = Image.open(fp) im = Image.open(fp)
@ -475,6 +478,7 @@ Reading from binary data
from PIL import Image from PIL import Image
import io import io
im = Image.open(io.BytesIO(buffer)) im = Image.open(io.BytesIO(buffer))
Note that the library rewinds the file (using ``seek(0)``) before reading the Note that the library rewinds the file (using ``seek(0)``) before reading the

View File

@ -87,10 +87,13 @@ true color.
Image.register_open(SpamImageFile.format, SpamImageFile, _accept) Image.register_open(SpamImageFile.format, SpamImageFile, _accept)
Image.register_extensions(SpamImageFile.format, [ Image.register_extensions(
SpamImageFile.format,
[
".spam", ".spam",
".spa", # DOS version ".spa", # DOS version
]) ],
)
The format handler must always set the The format handler must always set the
@ -111,6 +114,7 @@ Once the plugin has been imported, it can be used:
from PIL import Image from PIL import Image
import SpamImagePlugin import SpamImagePlugin
with Image.open("hopper.spam") as im: with Image.open("hopper.spam") as im:
pass pass
@ -163,16 +167,16 @@ TIFF, and many others. To use the raw decoder with the
image = Image.frombytes( image = Image.frombytes(
mode, size, data, "raw", mode, size, data, "raw",
raw mode, stride, orientation raw_mode, stride, orientation
) )
When used in a tile descriptor, the parameter field should look like:: When used in a tile descriptor, the parameter field should look like::
(raw mode, stride, orientation) (raw_mode, stride, orientation)
The fields are used as follows: The fields are used as follows:
**raw mode** **raw_mode**
The pixel layout used in the file, and is used to properly convert data to The pixel layout used in the file, and is used to properly convert data to
PILs internal layout. For a summary of the available formats, see the PILs internal layout. For a summary of the available formats, see the
table below. table below.

View File

@ -81,6 +81,7 @@ Example: Draw Partial Opacity Text
.. code-block:: python .. code-block:: python
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont
# get an image # get an image
with Image.open("Pillow/Tests/images/hopper.png").convert("RGBA") as base: with Image.open("Pillow/Tests/images/hopper.png").convert("RGBA") as base:
@ -557,7 +558,9 @@ Methods
.. code-block:: python .. code-block:: python
hello = draw.textlength("HelloW", font) - draw.textlength("W", font) # adjusted for kerning hello = draw.textlength("HelloW", font) - draw.textlength(
"W", font
) # adjusted for kerning
world = draw.textlength("World", font) world = draw.textlength("World", font)
hello_world = hello + world # adjusted for kerning hello_world = hello + world # adjusted for kerning
assert hello_world == draw.textlength("HelloWorld", font) # True assert hello_world == draw.textlength("HelloWorld", font) # True

View File

@ -17,7 +17,8 @@ changes it.
.. code-block:: python .. code-block:: python
from PIL import Image from PIL import Image
with Image.open('hopper.jpg') as im:
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)

View File

@ -18,7 +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
with Image.open('hopper.jpg') as im:
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)

View File

@ -63,6 +63,7 @@ Take your test image, and make a really simple harness.
:: ::
from PIL import Image from PIL import Image
with Image.open(path) as im: with Image.open(path) as im:
im.load() im.load()

View File

@ -14,17 +14,17 @@ The following are all equivalent::
import io import io
import pathlib import pathlib
with Image.open('test.jpg') as im: with Image.open("test.jpg") as im:
... ...
with Image.open(pathlib.Path('test.jpg')) as im2: with Image.open(pathlib.Path("test.jpg")) as im2:
... ...
with open('test.jpg', 'rb') as f: with open("test.jpg", "rb") as f:
im3 = Image.open(f) im3 = Image.open(f)
... ...
with open('test.jpg', 'rb') as f: with open("test.jpg", "rb") as f:
im4 = Image.open(io.BytesIO(f.read())) im4 = Image.open(io.BytesIO(f.read()))
... ...
@ -90,11 +90,11 @@ Complications
* After a file has been closed, operations that require file access will fail:: * After a file has been closed, operations that require file access will fail::
with open('test.jpg', 'rb') as f: with open("test.jpg", "rb") as f:
im5 = Image.open(f) im5 = Image.open(f)
im5.load() # FAILS, closed file im5.load() # FAILS, closed file
with Image.open('test.jpg') as im6: with Image.open("test.jpg") as im6:
pass pass
im6.load() # FAILS, closed file im6.load() # FAILS, closed file