Typing: UNDONE-- Mostly complete paste with working type annotations

This commit is contained in:
Eric Soroos 2018-01-04 14:09:13 +00:00
parent a0bdadf012
commit 6e45d8d577
2 changed files with 45 additions and 25 deletions

View File

@ -1393,7 +1393,8 @@ class Image(object):
def paste(self, im, box=None, mask=None): def paste(self, im, box=None, mask=None):
# type: (Union[Image, Color, Text], Optional[Union[LURD, Coord]], Optional[Image]) -> None # type: (Union[Image, Color, Text], Optional[Union[LURD, Coord]], Optional[Image]) -> None
## FIXME TYPING: Check this ^ # Alternate (frowned upon) positional type signaure
## type: (Union[Image, Color, Text], Image) -> None
""" """
Pastes another image into this image. The box argument is either Pastes another image into this image. The box argument is either
a 2-tuple giving the upper left corner, a 4-tuple defining the a 2-tuple giving the upper left corner, a 4-tuple defining the
@ -1433,46 +1434,65 @@ class Image(object):
:param mask: An optional mask image. :param mask: An optional mask image.
""" """
mask_img = None # type: Optional[Image]
box_lurd = None # type: Optional[LURD]
box_coord = None # type: Optional[Coord]
paste_color = None # type: Optional[Color]
paste_imcore = None # type: Optional[ImagingCore]
paste_im = None # type: Optional[Image]
if isImageType(box) and mask is None: if isImageType(box) and mask is None:
# abbreviated paste(im, mask) syntax # abbreviated paste(im, mask) syntax
mask = box mask_img = box # type: ignore
box = None box = None
if box is None: if box is None:
box = (0, 0) box = (0, 0)
if len(box) == 2: if isImageType(mask):
mask_img = mask
if isinstance(im, Image):
paste_im = im
paste_im.load()
if self.mode != paste_im.mode:
if self.mode != "RGB" or paste_im.mode not in ("RGBA", "RGBa"):
# should use an adapter for this!
paste_im = paste_im.convert(self.mode)
paste_imcore = paste_im.im
elif isStringType(im):
from . import ImageColor
paste_color = ImageColor.getcolor(im, self.mode)
elif isinstance(im, (int, float, tuple)):
paste_color = im
else:
raise ValueError("Incorrect Paste specification")
if len(box) == 4:
box_lurd = box
elif len(box) == 2:
# upper left corner given; get size from image or mask # upper left corner given; get size from image or mask
if isImageType(im): box_coord = box
size = im.size if paste_im:
elif isImageType(mask): size = paste_im.size
size = mask.size elif mask_img:
size = mask_img.size
else: else:
# FIXME: use self.size here? # FIXME: use self.size here?
raise ValueError( raise ValueError(
"cannot determine region size; use 4-item box" "cannot determine region size; use 4-item box"
) )
box += (box[0]+size[0], box[1]+size[1]) box_lurd = box_coord + (box_coord[0]+size[0], box_coord[1]+size[1])
else:
if isStringType(im): raise ValueError("Incorrect Box specification")
from . import ImageColor
im = ImageColor.getcolor(im, self.mode)
elif isImageType(im):
im.load()
if self.mode != im.mode:
if self.mode != "RGB" or im.mode not in ("RGBA", "RGBa"):
# should use an adapter for this!
im = im.convert(self.mode)
im = im.im
self._ensure_mutable() self._ensure_mutable()
if mask: if mask_img:
mask.load() mask_img.load()
self.im.paste(im, box, mask.im) self.im.paste(paste_imcore or paste_color, box_lurd, mask_img.im)
else: else:
self.im.paste(im, box) self.im.paste(paste_imcore or paste_color, box_lurd)
def alpha_composite(self, im, dest=(0,0), source=(0,0)): def alpha_composite(self, im, dest=(0,0), source=(0,0)):
# type: (Image, Coord, Union[Coord, LURD]) -> None # type: (Image, Coord, Union[Coord, LURD]) -> None

View File

@ -57,7 +57,7 @@ class ImagingCore:
mask: ImagingCore=...) -> List[int]: ... ### Do they take None? mask: ImagingCore=...) -> List[int]: ... ### Do they take None?
def modefilter(self, i: int) -> Any: ... def modefilter(self, i: int) -> Any: ...
# def offset(self): ... ### Function removed? # def offset(self): ... ### Function removed?
def paste(self, core: ImagingCore, box: LURD, coremask: ImagingCore) -> None: ... def paste(self, core: Union[ImagingCore, Color], box: LURD, coremask: ImagingCore=...) -> None: ...
def point(self, lut: Union[List[int], List[float]], mode: Optional[Mode]) -> ImagingCore: ... def point(self, lut: Union[List[int], List[float]], mode: Optional[Mode]) -> ImagingCore: ...
def point_transform(self, scale: float=1.0, offset: float=0.0) -> ImagingCore: ... def point_transform(self, scale: float=1.0, offset: float=0.0) -> ImagingCore: ...
def putdata(self, data: Any, scale: float=1.0, offset: float=0.0) -> None: ... def putdata(self, data: Any, scale: float=1.0, offset: float=0.0) -> None: ...