mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-27 17:54:32 +03:00
Apply black formatting to code examples
This commit is contained in:
parent
bd00cd9214
commit
675c5e1a9c
|
@ -75,9 +75,9 @@ method with the following parameters to affect how Ghostscript renders the EPS
|
||||||
relative position of the bounding box is maintained::
|
relative position of the bounding box is maintained::
|
||||||
|
|
||||||
im = Image.open(...)
|
im = Image.open(...)
|
||||||
im.size #(100,100)
|
im.size # (100,100)
|
||||||
im.load(scale=2)
|
im.load(scale=2)
|
||||||
im.size #(200,200)
|
im.size # (200,200)
|
||||||
|
|
||||||
**transparency**
|
**transparency**
|
||||||
If true, generates an RGBA image with a transparent background, instead of
|
If true, generates an RGBA image with a transparent background, instead of
|
||||||
|
@ -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)
|
||||||
|
|
|
@ -176,12 +176,13 @@ 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))
|
||||||
image.paste(part1, (xsize-delta, 0, xsize, ysize))
|
image.paste(part1, (xsize - delta, 0, xsize, ysize))
|
||||||
image.paste(part2, (0, 0, xsize-delta, ysize))
|
image.paste(part2, (0, 0, xsize - delta, ysize))
|
||||||
|
|
||||||
return image
|
return image
|
||||||
|
|
||||||
|
@ -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")
|
||||||
|
|
||||||
|
@ -382,14 +384,14 @@ Reading sequences
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
with Image.open("animation.gif") as im:
|
with Image.open("animation.gif") as im:
|
||||||
im.seek(1) # skip to the second frame
|
im.seek(1) # skip to the second frame
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while 1:
|
while 1:
|
||||||
im.seek(im.tell()+1)
|
im.seek(im.tell() + 1)
|
||||||
# do something to im
|
# do something to im
|
||||||
except EOFError:
|
except EOFError:
|
||||||
pass # end of sequence
|
pass # end of sequence
|
||||||
|
|
||||||
As seen in this example, you’ll get an :py:exc:`EOFError` exception when the
|
As seen in this example, you’ll get an :py:exc:`EOFError` exception when the
|
||||||
sequence ends.
|
sequence ends.
|
||||||
|
@ -422,9 +424,9 @@ Drawing PostScript
|
||||||
|
|
||||||
with Image.open("hopper.ppm") as im:
|
with Image.open("hopper.ppm") as im:
|
||||||
title = "hopper"
|
title = "hopper"
|
||||||
box = (1*72, 2*72, 7*72, 10*72) # in points
|
box = (1 * 72, 2 * 72, 7 * 72, 10 * 72) # in points
|
||||||
|
|
||||||
ps = PSDraw.PSDraw() # default is sys.stdout or sys.stdout.buffer
|
ps = PSDraw.PSDraw() # default is sys.stdout or sys.stdout.buffer
|
||||||
ps.begin_document(title)
|
ps.begin_document(title)
|
||||||
|
|
||||||
# draw the image (75 dpi)
|
# draw the image (75 dpi)
|
||||||
|
@ -433,7 +435,7 @@ Drawing PostScript
|
||||||
|
|
||||||
# draw title
|
# draw title
|
||||||
ps.setfont("HelveticaNarrow-Bold", 36)
|
ps.setfont("HelveticaNarrow-Bold", 36)
|
||||||
ps.text((3*72, 4*72), title)
|
ps.text((3 * 72, 4 * 72), title)
|
||||||
|
|
||||||
ps.end_document()
|
ps.end_document()
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
|
|
@ -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(
|
||||||
".spam",
|
SpamImageFile.format,
|
||||||
".spa", # DOS version
|
[
|
||||||
])
|
".spam",
|
||||||
|
".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
|
||||||
|
|
||||||
|
|
|
@ -81,11 +81,12 @@ 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:
|
||||||
|
|
||||||
# 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)
|
||||||
|
@ -93,9 +94,9 @@ Example: Draw Partial Opacity Text
|
||||||
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)
|
||||||
|
|
||||||
|
@ -117,7 +118,7 @@ Example: Draw Multiline Text
|
||||||
d = ImageDraw.Draw(out)
|
d = ImageDraw.Draw(out)
|
||||||
|
|
||||||
# draw multiline text
|
# draw multiline text
|
||||||
d.multiline_text((10,10), "Hello\nWorld", font=fnt, fill=(0, 0, 0))
|
d.multiline_text((10, 10), "Hello\nWorld", font=fnt, fill=(0, 0, 0))
|
||||||
|
|
||||||
out.show()
|
out.show()
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
|
|
@ -17,11 +17,12 @@ 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)
|
||||||
print (px[4,4])
|
print(px[4, 4])
|
||||||
|
|
||||||
Results in the following::
|
Results in the following::
|
||||||
|
|
||||||
|
@ -32,8 +33,8 @@ Access using negative indexes is also possible.
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
px[-1,-1] = (0,0,0)
|
px[-1, -1] = (0, 0, 0)
|
||||||
print (px[-1,-1])
|
print(px[-1, -1])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -18,11 +18,12 @@ 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)
|
||||||
print (px[4,4])
|
print(px[4, 4])
|
||||||
|
|
||||||
Results in the following::
|
Results in the following::
|
||||||
|
|
||||||
|
@ -33,8 +34,8 @@ Access using negative indexes is also possible.
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
px[-1,-1] = (0,0,0)
|
px[-1, -1] = (0, 0, 0)
|
||||||
print (px[-1,-1])
|
print(px[-1, -1])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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()
|
||||||
|
|
||||||
|
|
|
@ -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()))
|
||||||
...
|
...
|
||||||
|
|
||||||
|
@ -65,10 +65,10 @@ Image Lifecycle
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
with Image.open("test.jpg") as img:
|
with Image.open("test.jpg") as img:
|
||||||
img.load()
|
img.load()
|
||||||
assert img.fp is None
|
assert img.fp is None
|
||||||
img.save("test.png")
|
img.save("test.png")
|
||||||
|
|
||||||
|
|
||||||
The lifecycle of a single-frame image is relatively simple. The file must
|
The lifecycle of a single-frame image is relatively simple. The file must
|
||||||
|
@ -90,13 +90,13 @@ 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
|
||||||
|
|
||||||
|
|
||||||
Proposed File Handling
|
Proposed File Handling
|
||||||
|
|
Loading…
Reference in New Issue
Block a user