Added source point parameter for Image.Image.alpha_composite

This commit is contained in:
wiredfool 2017-06-26 20:04:44 +01:00
parent 69768c514b
commit 1d33dfbced
2 changed files with 27 additions and 7 deletions

View File

@ -1373,16 +1373,27 @@ class Image(object):
else: else:
self.im.paste(im, box) self.im.paste(im, box)
def alpha_composite(self, im, box=None): def alpha_composite(self, im, box=None, 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. If a 2 tuple, the upper :param box: Optional 2 or 4 tuple specificing the bounds in
left corner. If 4 tuple, (x0,y0,x1,y1) the destination image. If a 2 tuple, the upper left
corner. If 4 tuple, (x0,y0,x1,y1)
:param source: Optional 2 tuple, (x0, y0) for the upper left
corner in the source image.
Note: Not currently implemented in-place. Note: Not currently implemented in-place.
""" """
if box is None:
if not isinstance(source, tuple):
raise ValueError("Source must be a tuple")
if not len(source) == 2:
raise ValueError("Source must be a 2-tuple")
if min(source) < 0:
raise ValueError("Source must be non-negative")
if box is None and source == (0, 0):
box = (0, 0, im.width, im.height) box = (0, 0, im.width, im.height)
overlay = im overlay = im
if self.size == im.size: if self.size == im.size:
@ -1396,7 +1407,9 @@ class Image(object):
box += (box[0]+size[0], box[1]+size[1]) box += (box[0]+size[0], box[1]+size[1])
src = self.crop(box) src = self.crop(box)
overlay = im.crop((0, 0, box[2]-box[0], box[3]-box[1])) overlay = im.crop((source[0], source[1],
source[0] + box[2] - box[0],
source[1] + box[3] - box[1]))
result = alpha_composite(src, overlay) result = alpha_composite(src, overlay)
self.paste(result, box) self.paste(result, box)

View File

@ -231,7 +231,14 @@ class TestImage(PillowTestCase):
self.assert_image_equal(box.crop((96, 96, 128, 128)), self.assert_image_equal(box.crop((96, 96, 128, 128)),
src.crop((0, 0, 32, 32))) src.crop((0, 0, 32, 32)))
self.assertEqual(box.size, (128, 128)) self.assertEqual(box.size, (128, 128))
# source point
source = src.copy()
source.alpha_composite(over, (32, 32, 96, 96), (32, 32))
self.assert_image_equal(source.crop((32, 32, 96, 96)),
target.crop((32, 32, 96, 96)))
self.assertEqual(source.size, (128, 128))
def test_registered_extensions_uninitialized(self): def test_registered_extensions_uninitialized(self):
# Arrange # Arrange