From dce04b680bdb0be3335826e5feb7d0bcba00777e Mon Sep 17 00:00:00 2001 From: wiredfool Date: Sat, 11 Nov 2017 16:31:37 +0000 Subject: [PATCH 1/4] Pass in fill color to transform. --- PIL/Image.py | 13 ++++++++----- Tests/test_image_transform.py | 23 ++++++++++++++++++++++- _imaging.c | 4 ++-- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index 70a8582f605..d8e230b1652 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -2074,7 +2074,7 @@ def thumbnail(self, size, resample=BICUBIC): # FIXME: the different transform methods need further explanation # instead of bloating the method docs, add a separate chapter. - def transform(self, size, method, data=None, resample=NEAREST, fill=1): + def transform(self, size, method, data=None, resample=NEAREST, fill=None): """ Transforms this image. This method creates a new image with the given size, and the same mode as the original, and copies data @@ -2095,9 +2095,11 @@ def transform(self, size, method, data=None, resample=NEAREST, fill=1): environment), or :py:attr:`PIL.Image.BICUBIC` (cubic spline interpolation in a 4x4 environment). If omitted, or if the image has mode "1" or "P", it is set to :py:attr:`PIL.Image.NEAREST`. + :param fill: Optional fill color for the area outside the transform + in the output image. :returns: An :py:class:`~PIL.Image.Image` object. """ - + if self.mode == 'LA': return self.convert('La').transform( size, method, data, resample, fill).convert('LA') @@ -2107,6 +2109,7 @@ def transform(self, size, method, data=None, resample=NEAREST, fill=1): size, method, data, resample, fill).convert('RGBA') if isinstance(method, ImageTransformHandler): + fill = 1 return method.transform(size, self, resample=resample, fill=fill) if hasattr(method, "getdata"): @@ -2116,13 +2119,13 @@ def transform(self, size, method, data=None, resample=NEAREST, fill=1): if data is None: raise ValueError("missing method data") - im = new(self.mode, size, None) + im = new(self.mode, size, fill) if method == MESH: # list of quads for box, quad in data: - im.__transformer(box, self, QUAD, quad, resample, fill) + im.__transformer(box, self, QUAD, quad, resample, fill is None) else: - im.__transformer((0, 0)+size, self, method, data, resample, fill) + im.__transformer((0, 0)+size, self, method, data, resample, fill is None) return im diff --git a/Tests/test_image_transform.py b/Tests/test_image_transform.py index e03cfe24a51..bb8a0ddfacf 100644 --- a/Tests/test_image_transform.py +++ b/Tests/test_image_transform.py @@ -52,6 +52,17 @@ def test_quad(self): self.assert_image_equal(transformed, scaled) + def test_fill(self): + im = hopper('RGB') + (w, h) = im.size + transformed = im.transform(im.size, Image.EXTENT, + (0, 0, + w*2, h*2), + Image.BILINEAR, + fill = 'red') + + self.assertEqual(transformed.getpixel((w-1,h-1)), (255,0,0)) + def test_mesh(self): # this should be a checkerboard of halfsized hoppers in ul, lr im = hopper('RGBA') @@ -256,6 +267,16 @@ class TestImageTransformPerspective(TestImageTransformAffine): # Repeat all tests for AFFINE transformations with PERSPECTIVE transform = Image.PERSPECTIVE - +class TestFill(unittest.TestCase): + def test_fill(self): + im = hopper('RGB') + (w, h) = im.size + transformed = im.transform(im.size, Image.EXTENT, + (0, 0, + w*2, h*2), + Image.BILINEAR, + fill = 'red') + transformed.show() + self.assertEqual(transformed.getpixel((w-1,h-1)), (255,0,0)) if __name__ == '__main__': unittest.main() diff --git a/_imaging.c b/_imaging.c index ec90e1f7fb6..f13ec35dc2a 100644 --- a/_imaging.c +++ b/_imaging.c @@ -1612,7 +1612,7 @@ _transform2(ImagingObject* self, PyObject* args) int fill = 1; if (!PyArg_ParseTuple(args, "(iiii)O!iO|ii", &x0, &y0, &x1, &y1, - &Imaging_Type, &imagep, + &Imaging_Type, &imagep, &method, &data, &filter, &fill)) return NULL; @@ -1637,7 +1637,7 @@ _transform2(ImagingObject* self, PyObject* args) imOut = ImagingTransform( self->image, imagep->image, method, - x0, y0, x1, y1, a, filter, 1); + x0, y0, x1, y1, a, filter, fill); free(a); From 68b960ca2fad759410c8cb96954dae41411a7b0b Mon Sep 17 00:00:00 2001 From: wiredfool Date: Sat, 11 Nov 2017 16:53:24 +0000 Subject: [PATCH 2/4] Use different parameter than the existing interface --- PIL/Image.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index d8e230b1652..0ff3c8a8045 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -2074,7 +2074,8 @@ def thumbnail(self, size, resample=BICUBIC): # FIXME: the different transform methods need further explanation # instead of bloating the method docs, add a separate chapter. - def transform(self, size, method, data=None, resample=NEAREST, fill=None): + def transform(self, size, method, data=None, resample=NEAREST, + fill=1, fillcolor=None): """ Transforms this image. This method creates a new image with the given size, and the same mode as the original, and copies data @@ -2095,7 +2096,7 @@ def transform(self, size, method, data=None, resample=NEAREST, fill=None): environment), or :py:attr:`PIL.Image.BICUBIC` (cubic spline interpolation in a 4x4 environment). If omitted, or if the image has mode "1" or "P", it is set to :py:attr:`PIL.Image.NEAREST`. - :param fill: Optional fill color for the area outside the transform + :param fillcolor: Optional fill color for the area outside the transform in the output image. :returns: An :py:class:`~PIL.Image.Image` object. """ @@ -2109,7 +2110,6 @@ def transform(self, size, method, data=None, resample=NEAREST, fill=None): size, method, data, resample, fill).convert('RGBA') if isinstance(method, ImageTransformHandler): - fill = 1 return method.transform(size, self, resample=resample, fill=fill) if hasattr(method, "getdata"): @@ -2119,13 +2119,15 @@ def transform(self, size, method, data=None, resample=NEAREST, fill=None): if data is None: raise ValueError("missing method data") - im = new(self.mode, size, fill) + im = new(self.mode, size, fillcolor) if method == MESH: # list of quads for box, quad in data: - im.__transformer(box, self, QUAD, quad, resample, fill is None) + im.__transformer(box, self, QUAD, quad, resample, + fillcolor is None) else: - im.__transformer((0, 0)+size, self, method, data, resample, fill is None) + im.__transformer((0, 0)+size, self, method, data, + resample, fillcolor is None) return im From 52b68948653d6e079d8edc3e36ccbf37c710c4ae Mon Sep 17 00:00:00 2001 From: Eric Soroos Date: Mon, 13 Nov 2017 11:59:12 +0000 Subject: [PATCH 3/4] parameter name change --- Tests/test_image_transform.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/test_image_transform.py b/Tests/test_image_transform.py index bb8a0ddfacf..7d25b79e331 100644 --- a/Tests/test_image_transform.py +++ b/Tests/test_image_transform.py @@ -59,7 +59,7 @@ def test_fill(self): (0, 0, w*2, h*2), Image.BILINEAR, - fill = 'red') + fillcolor = 'red') self.assertEqual(transformed.getpixel((w-1,h-1)), (255,0,0)) From fa9e9c2795e845a2447a2191390bffcd08a41a7e Mon Sep 17 00:00:00 2001 From: Eric Soroos Date: Mon, 13 Nov 2017 11:59:26 +0000 Subject: [PATCH 4/4] remove duplicate test --- Tests/test_image_transform.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/Tests/test_image_transform.py b/Tests/test_image_transform.py index 7d25b79e331..df8fc83e84a 100644 --- a/Tests/test_image_transform.py +++ b/Tests/test_image_transform.py @@ -267,16 +267,5 @@ class TestImageTransformPerspective(TestImageTransformAffine): # Repeat all tests for AFFINE transformations with PERSPECTIVE transform = Image.PERSPECTIVE -class TestFill(unittest.TestCase): - def test_fill(self): - im = hopper('RGB') - (w, h) = im.size - transformed = im.transform(im.size, Image.EXTENT, - (0, 0, - w*2, h*2), - Image.BILINEAR, - fill = 'red') - transformed.show() - self.assertEqual(transformed.getpixel((w-1,h-1)), (255,0,0)) if __name__ == '__main__': unittest.main()