Added type hints to example code

This commit is contained in:
Andrew Murray 2024-08-15 14:10:31 +10:00
parent 8afb7ddb4e
commit d4c72da6b2
3 changed files with 13 additions and 11 deletions

View File

@ -1517,19 +1517,21 @@ To add other read or write support, use
:py:func:`PIL.WmfImagePlugin.register_handler` to register a WMF and EMF
handler. ::
from PIL import Image
from typing import IO
from PIL import Image, ImageFile
from PIL import WmfImagePlugin
class WmfHandler:
def open(self, im):
class WmfHandler(ImageFile.StubHandler):
def open(self, im: ImageFile.StubImageFile) -> None:
...
def load(self, im):
def load(self, im: ImageFile.StubImageFile) -> Image.Image:
...
return image
def save(self, im, fp, filename):
def save(self, im: Image.Image, fp: IO[bytes], filename: str) -> None:
...

View File

@ -186,7 +186,7 @@ Rolling an image
::
def roll(im, delta):
def roll(im: Image.Image, delta: int) -> Image.Image:
"""Roll an image sideways."""
xsize, ysize = im.size
@ -211,7 +211,7 @@ Merging images
::
def merge(im1, im2):
def merge(im1: Image.Image, im2: Image.Image) -> Image.Image:
w = im1.size[0] + im2.size[0]
h = max(im1.size[1], im2.size[1])
im = Image.new("RGBA", (w, h))
@ -704,7 +704,7 @@ in the current directory can be saved as JPEGs at reduced quality.
import glob
from PIL import Image
def compress_image(source_path, dest_path):
def compress_image(source_path: str, dest_path: str) -> None:
with Image.open(source_path) as img:
if img.mode != "RGB":
img = img.convert("RGB")

View File

@ -53,7 +53,7 @@ true color.
from PIL import Image, ImageFile
def _accept(prefix):
def _accept(prefix: bytes) -> bool:
return prefix[:4] == b"SPAM"
@ -62,7 +62,7 @@ true color.
format = "SPAM"
format_description = "Spam raster image"
def _open(self):
def _open(self) -> None:
header = self.fp.read(128).split()
@ -82,7 +82,7 @@ true color.
raise SyntaxError(msg)
# data descriptor
self.tile = [("raw", (0, 0) + self.size, 128, (self.mode, 0, 1))]
self.tile = [ImageFile._Tile("raw", (0, 0) + self.size, 128, (self.mode, 0, 1))]
Image.register_open(SpamImageFile.format, SpamImageFile, _accept)