From 7bae0d40f0d47174cf1bc66d4068a5212ba7abf7 Mon Sep 17 00:00:00 2001 From: Jae-Myoung Yu Date: Wed, 22 Apr 2015 04:23:12 +0900 Subject: [PATCH 1/2] Added Image.extent --- tests/image_test.py | 15 +++++++++++++++ wand/api.py | 6 ++++++ wand/image.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/tests/image_test.py b/tests/image_test.py index 75f063f1..ca7bc9ca 100644 --- a/tests/image_test.py +++ b/tests/image_test.py @@ -664,6 +664,21 @@ def test_crop_error(fx_asset): img.crop(bottom=1, height=2) +def test_extent(fx_asset): + with Image(filename=str(fx_asset.join('croptest.png'))) as img: + with img.clone() as extended: + assert extended.size == img.size + extended.extent(width=500) + assert extended.width == 500 + assert extended.height == img.height + + with img.clone() as extended: + assert extended.size == img.size + extended.extent(height=500) + assert extended.width == img.width + assert extended.height == 500 + + @mark.parametrize(('method'), [ ('resize'), ('sample'), diff --git a/wand/api.py b/wand/api.py index a826f0f3..3922af9f 100644 --- a/wand/api.py +++ b/wand/api.py @@ -498,6 +498,12 @@ class AffineMatrix(ctypes.Structure): library.MagickTrimImage.argtypes = [ctypes.c_void_p, ctypes.c_double] + library.MagickExtentImage.argtypes = [ctypes.c_void_p, + ctypes.c_size_t, + ctypes.c_size_t, + ctypes.c_ssize_t, + ctypes.c_ssize_t] + library.MagickGaussianBlurImage.argtypes = [ctypes.c_void_p, ctypes.c_double, ctypes.c_double] diff --git a/wand/image.py b/wand/image.py index b11357ca..5da6ef28 100644 --- a/wand/image.py +++ b/wand/image.py @@ -1165,6 +1165,38 @@ def abs_(n, m, null=None): if reset_coords: self.reset_coords() + @manipulative + def extent(self, width=None, height=None, x=0, y=0): + """extends the image as defined by the geometry, gravity, and wand + background color. Set the (x,y) offset of the geometry to move the + original wand relative to the extended wand. + + :param width: the :attr:`width` of the extended image. + default is the :attr:`width` of the image. + :type width: :class:`numbers.Integral` + :param height: the :attr:`height` of the extended image. + default is the :attr:`height` of the image. + :type height: :class:`numbers.Integral` + :param x: the :attr:`x` offset of the extended image. + default is 0 + :type x: :class:`numbers.Integral` + :param y: the :attr:`y` offset of the extended image. + default is 0 + :type y: :class:`numbers.Integral` + """ + if width is None: + width = self.width + if height is None: + height = self.height + if width < 1: + raise ValueError('image width cannot be zero') + elif height < 1: + raise ValueError('image height cannot be zero') + + result = library.MagickExtentImage(self.wand, width, height, x, y) + if not result: + self.raise_exception() + def reset_coords(self): """Reset the coordinate frame of the image so to the upper-left corner is (0, 0) again (crop and rotate operations change it). From 5cea4cfe54781998f5aabf6f8cfc162883c2c815 Mon Sep 17 00:00:00 2001 From: Jae-Myoung Yu Date: Wed, 22 Apr 2015 21:15:17 +0900 Subject: [PATCH 2/2] Added tests for Image.extent --- tests/image_test.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/image_test.py b/tests/image_test.py index ca7bc9ca..eca2c8a8 100644 --- a/tests/image_test.py +++ b/tests/image_test.py @@ -678,6 +678,12 @@ def test_extent(fx_asset): assert extended.width == img.width assert extended.height == 500 + with raises(ValueError): + img.extent(width=0) + + with raises(ValueError): + img.extent(height=0) + @mark.parametrize(('method'), [ ('resize'),