mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-10 19:56:47 +03:00
Raise an error when performing a negative crop
This commit is contained in:
parent
83d369a347
commit
af90dd773b
|
@ -86,21 +86,12 @@ class TestDecompressionCrop:
|
|||
pytest.warns(Image.DecompressionBombWarning, src.crop, box)
|
||||
|
||||
def test_crop_decompression_checks(self):
|
||||
|
||||
im = Image.new("RGB", (100, 100))
|
||||
|
||||
good_values = ((-9999, -9999, -9990, -9990), (-999, -999, -990, -990))
|
||||
|
||||
warning_values = ((-160, -160, 99, 99), (160, 160, -99, -99))
|
||||
|
||||
error_values = ((-99909, -99990, 99999, 99999), (99909, 99990, -99999, -99999))
|
||||
|
||||
for value in good_values:
|
||||
for value in ((-9999, -9999, -9990, -9990), (-999, -999, -990, -990)):
|
||||
assert im.crop(value).size == (9, 9)
|
||||
|
||||
for value in warning_values:
|
||||
pytest.warns(Image.DecompressionBombWarning, im.crop, value)
|
||||
pytest.warns(Image.DecompressionBombWarning, im.crop, (-160, -160, 99, 99))
|
||||
|
||||
for value in error_values:
|
||||
with pytest.raises(Image.DecompressionBombError):
|
||||
im.crop(value)
|
||||
with pytest.raises(Image.DecompressionBombError):
|
||||
im.crop((-99909, -99990, 99999, 99999))
|
||||
|
|
|
@ -47,16 +47,12 @@ def test_wide_crop():
|
|||
assert crop(-25, 75, 25, 125) == (1875, 625)
|
||||
|
||||
|
||||
def test_negative_crop():
|
||||
# Check negative crop size (@PIL171)
|
||||
@pytest.mark.parametrize("box", ((8, 2, 2, 8), (2, 8, 8, 2), (8, 8, 2, 2)))
|
||||
def test_negative_crop(box):
|
||||
im = Image.new("RGB", (10, 10))
|
||||
|
||||
im = Image.new("L", (512, 512))
|
||||
im = im.crop((400, 400, 200, 200))
|
||||
|
||||
assert im.size == (0, 0)
|
||||
assert len(im.getdata()) == 0
|
||||
with pytest.raises(IndexError):
|
||||
im.getdata()[0]
|
||||
with pytest.raises(ValueError):
|
||||
im.crop(box)
|
||||
|
||||
|
||||
def test_crop_float():
|
||||
|
|
|
@ -1145,6 +1145,11 @@ class Image:
|
|||
if box is None:
|
||||
return self.copy()
|
||||
|
||||
if box[2] < box[0]:
|
||||
raise ValueError("Region right less than region left")
|
||||
elif box[3] < box[1]:
|
||||
raise ValueError("Region lower less than region upper")
|
||||
|
||||
self.load()
|
||||
return self._new(self._crop(self.im, box))
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user