Skip to content

Commit

Permalink
Merge pull request #5972 from radarhere/crop
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Jan 21, 2022
2 parents 48d32fe + 591231b commit 53d0dd5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
17 changes: 4 additions & 13 deletions Tests/test_decompression_bomb.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,12 @@ def testEnlargeCrop(self):
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))
14 changes: 5 additions & 9 deletions Tests/test_image_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,12 @@ def crop(*bbox):
assert crop(-25, 75, 25, 125) == (1875, 625)


def test_negative_crop():
# Check negative crop size (@PIL171)

im = Image.new("L", (512, 512))
im = im.crop((400, 400, 200, 200))
@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))

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():
Expand Down
5 changes: 5 additions & 0 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,11 @@ def crop(self, box=None):
if box is None:
return self.copy()

if box[2] < box[0]:
raise ValueError("Coordinate 'right' is less than 'left'")
elif box[3] < box[1]:
raise ValueError("Coordinate 'lower' is less than 'upper'")

self.load()
return self._new(self._crop(self.im, box))

Expand Down

0 comments on commit 53d0dd5

Please sign in to comment.