mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-02-03 21:24:31 +03:00
add roi argument to Image.resize() method
constraints check
This commit is contained in:
parent
e7569a1ee9
commit
f828416752
|
@ -1519,7 +1519,7 @@ class Image(object):
|
|||
return self.pyaccess.putpixel(xy, value)
|
||||
return self.im.putpixel(xy, value)
|
||||
|
||||
def resize(self, size, resample=NEAREST):
|
||||
def resize(self, size, resample=NEAREST, roi=None):
|
||||
"""
|
||||
Returns a resized copy of this image.
|
||||
|
||||
|
@ -1555,7 +1555,10 @@ class Image(object):
|
|||
if self.mode == 'RGBA':
|
||||
return self.convert('RGBa').resize(size, resample).convert('RGBA')
|
||||
|
||||
return self._new(self.im.resize(size, resample))
|
||||
if roi is None:
|
||||
roi = (0, 0) + self.size
|
||||
|
||||
return self._new(self.im.resize(size, resample, roi))
|
||||
|
||||
def rotate(self, angle, resample=NEAREST, expand=0):
|
||||
"""
|
||||
|
|
|
@ -348,5 +348,36 @@ class CoreResampleCoefficientsTest(PillowTestCase):
|
|||
self.assertEqual(histogram[0x100 * 3 + 0xff], 0x10000) # fourth channel
|
||||
|
||||
|
||||
class CoreResampleRoiTest(PillowTestCase):
|
||||
def test_wrong_arguments(self):
|
||||
im = hopper()
|
||||
for resample in (Image.LINEAR, Image.BOX, Image.BILINEAR, Image.HAMMING,
|
||||
Image.BICUBIC, Image.LANCZOS):
|
||||
im.resize((32, 32), resample, (0, 0, im.width, im.height))
|
||||
im.resize((32, 32), resample, (20, 20, im.width, im.height))
|
||||
|
||||
with self.assertRaisesRegexp(TypeError, "must be sequence of length 4"):
|
||||
im.resize((32, 32), resample, (im.width, im.height))
|
||||
|
||||
with self.assertRaisesRegexp(ValueError, "can't be negative"):
|
||||
im.resize((32, 32), resample, (-20, 20, 100, 100))
|
||||
with self.assertRaisesRegexp(ValueError, "can't be negative"):
|
||||
im.resize((32, 32), resample, (20, -20, 100, 100))
|
||||
|
||||
with self.assertRaisesRegexp(ValueError, "can't be empty"):
|
||||
im.resize((32, 32), resample, (20, 20, 20, 100))
|
||||
with self.assertRaisesRegexp(ValueError, "can't be empty"):
|
||||
im.resize((32, 32), resample, (100, 20, 20, 100))
|
||||
with self.assertRaisesRegexp(ValueError, "can't be empty"):
|
||||
im.resize((32, 32), resample, (20, 20, 100, 20))
|
||||
with self.assertRaisesRegexp(ValueError, "can't be empty"):
|
||||
im.resize((32, 32), resample, (20, 100, 100, 20))
|
||||
|
||||
with self.assertRaisesRegexp(ValueError, "can't exceed"):
|
||||
im.resize((32, 32), resample, (0, 0, im.width + 1, im.height))
|
||||
with self.assertRaisesRegexp(ValueError, "can't exceed"):
|
||||
im.resize((32, 32), resample, (0, 0, im.width, im.height + 1))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
23
_imaging.c
23
_imaging.c
|
@ -1534,15 +1534,32 @@ _resize(ImagingObject* self, PyObject* args)
|
|||
|
||||
int xsize, ysize;
|
||||
int filter = IMAGING_TRANSFORM_NEAREST;
|
||||
if (!PyArg_ParseTuple(args, "(ii)|i", &xsize, &ysize, &filter))
|
||||
return NULL;
|
||||
|
||||
float roi[4] = {0, 0, 0, 0};
|
||||
|
||||
imIn = self->image;
|
||||
roi[2] = imIn->xsize;
|
||||
roi[3] = imIn->ysize;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "(ii)|i(ffff)", &xsize, &ysize, &filter,
|
||||
&roi[0], &roi[1], &roi[2], &roi[3]))
|
||||
return NULL;
|
||||
|
||||
if (xsize < 1 || ysize < 1) {
|
||||
return ImagingError_ValueError("height and width must be > 0");
|
||||
}
|
||||
|
||||
if (roi[0] < 0 || roi[1] < 0) {
|
||||
return ImagingError_ValueError("region of interest offset can't be negative");
|
||||
}
|
||||
|
||||
if (roi[2] > imIn->xsize || roi[3] > imIn->ysize) {
|
||||
return ImagingError_ValueError("region of interest can't exceed original image size");
|
||||
}
|
||||
|
||||
if (roi[2] - roi[0] <= 0 || roi[3] - roi[1] <= 0) {
|
||||
return ImagingError_ValueError("region of interest can't be empty");
|
||||
}
|
||||
|
||||
if (imIn->xsize == xsize && imIn->ysize == ysize) {
|
||||
imOut = ImagingCopy(imIn);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user