mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-04-27 20:43:43 +03:00
re-organized the parameters, moved size specification to overlay tuple
This commit is contained in:
parent
1d33dfbced
commit
c7efd0bf6d
56
PIL/Image.py
56
PIL/Image.py
|
@ -1373,45 +1373,51 @@ class Image(object):
|
||||||
else:
|
else:
|
||||||
self.im.paste(im, box)
|
self.im.paste(im, box)
|
||||||
|
|
||||||
def alpha_composite(self, im, box=None, source=(0,0)):
|
def alpha_composite(self, im, dest=(0,0), source=(0,0)):
|
||||||
""" 'In-place' analog of Image.alpha_composite
|
""" 'In-place' analog of Image.alpha_composite
|
||||||
|
|
||||||
:param im: image to composite over this one
|
:param im: image to composite over this one
|
||||||
:param box: Optional 2 or 4 tuple specificing the bounds in
|
:param dest: Optional 2 tuple specificing the upper left corner
|
||||||
the destination image. If a 2 tuple, the upper left
|
in the this image.
|
||||||
corner. If 4 tuple, (x0,y0,x1,y1)
|
:param source: Optional 2 or 4 tuple, (x0, y0) for the upper
|
||||||
:param source: Optional 2 tuple, (x0, y0) for the upper left
|
left corner in the source image. If a 4 tuple, the bounds of
|
||||||
corner in the source image.
|
the source rectangle.
|
||||||
|
|
||||||
Note: Not currently implemented in-place.
|
Note: Not currently implemented in-place.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if not isinstance(source, tuple):
|
if not isinstance(source, tuple):
|
||||||
raise ValueError("Source must be a tuple")
|
raise ValueError("Source must be a tuple")
|
||||||
if not len(source) == 2:
|
if not isinstance(dest, tuple):
|
||||||
raise ValueError("Source must be a 2-tuple")
|
raise ValueError("Destination must be a tuple")
|
||||||
|
if not len(source) in (2, 4):
|
||||||
|
raise ValueError("Source must be a 2 or 4-tuple")
|
||||||
|
if not len(dest) == 2:
|
||||||
|
raise ValueError("Destination must be a 2-tuple")
|
||||||
if min(source) < 0:
|
if min(source) < 0:
|
||||||
raise ValueError("Source must be non-negative")
|
raise ValueError("Source must be non-negative")
|
||||||
|
if min(dest) < 0:
|
||||||
if box is None and source == (0, 0):
|
raise ValueError("Destination must be non-negative")
|
||||||
box = (0, 0, im.width, im.height)
|
|
||||||
|
if len(source) == 2:
|
||||||
|
source = source + im.size
|
||||||
|
|
||||||
|
# over image, crop if it's not the whole thing.
|
||||||
|
if source == (0,0) + im.size:
|
||||||
overlay = im
|
overlay = im
|
||||||
if self.size == im.size:
|
|
||||||
src = self
|
|
||||||
else:
|
|
||||||
src = self.crop(box)
|
|
||||||
else:
|
else:
|
||||||
if len(box) == 2:
|
overlay = im.crop(source)
|
||||||
# upper left corner given; get size from overlay image
|
|
||||||
size = im.size
|
# target for the paste
|
||||||
box += (box[0]+size[0], box[1]+size[1])
|
box = dest + (dest[0] + overlay.width, dest[1] + overlay.height)
|
||||||
|
|
||||||
src = self.crop(box)
|
|
||||||
overlay = im.crop((source[0], source[1],
|
|
||||||
source[0] + box[2] - box[0],
|
|
||||||
source[1] + box[3] - box[1]))
|
|
||||||
|
|
||||||
result = alpha_composite(src, overlay)
|
# destination image. don't copy if we're using the whole image.
|
||||||
|
if dest == (0,0) + self.size:
|
||||||
|
background = self
|
||||||
|
else:
|
||||||
|
background = self.crop(box)
|
||||||
|
|
||||||
|
result = alpha_composite(background, overlay)
|
||||||
self.paste(result, box)
|
self.paste(result, box)
|
||||||
|
|
||||||
def point(self, lut, mode=None):
|
def point(self, lut, mode=None):
|
||||||
|
|
|
@ -225,7 +225,7 @@ class TestImage(PillowTestCase):
|
||||||
|
|
||||||
# offset and crop
|
# offset and crop
|
||||||
box = src.copy()
|
box = src.copy()
|
||||||
box.alpha_composite(over, (64, 64, 96, 96))
|
box.alpha_composite(over, (64, 64), (0, 0, 32, 32))
|
||||||
self.assert_image_equal(box.crop((64, 64, 96, 96)),
|
self.assert_image_equal(box.crop((64, 64, 96, 96)),
|
||||||
target.crop((0, 0, 32, 32)))
|
target.crop((0, 0, 32, 32)))
|
||||||
self.assert_image_equal(box.crop((96, 96, 128, 128)),
|
self.assert_image_equal(box.crop((96, 96, 128, 128)),
|
||||||
|
@ -234,7 +234,7 @@ class TestImage(PillowTestCase):
|
||||||
|
|
||||||
# source point
|
# source point
|
||||||
source = src.copy()
|
source = src.copy()
|
||||||
source.alpha_composite(over, (32, 32, 96, 96), (32, 32))
|
source.alpha_composite(over, (32, 32), (32, 32, 96, 96))
|
||||||
|
|
||||||
self.assert_image_equal(source.crop((32, 32, 96, 96)),
|
self.assert_image_equal(source.crop((32, 32, 96, 96)),
|
||||||
target.crop((32, 32, 96, 96)))
|
target.crop((32, 32, 96, 96)))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user