mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-13 13:16:52 +03:00
7da17ad41e
The previous test configuration made it difficult to run a single test with the pytest CLI. There were two major issues: - The Tests directory was not a package. It now includes a __init__.py file and imports from other tests modules are done with relative imports. - setup.cfg always specified the Tests directory. So even if a specific test were specified as a CLI arg, this configuration would also always include all tests. This configuration has been removed to allow specifying a single test on the command line. Contributors can now run specific tests with a single command such as: $ tox -e py37 -- Tests/test_file_pdf.py::TestFilePdf.test_rgb This makes it easy and faster to iterate on a single test failure and is very familiar to those that have previously used tox and pytest. When running tox or pytest with no arguments, they still discover and runs all tests in the Tests directory.
109 lines
3.2 KiB
Python
109 lines
3.2 KiB
Python
from .helper import unittest, PillowTestCase, hopper
|
|
|
|
from PIL import Image
|
|
|
|
|
|
class TestImageCrop(PillowTestCase):
|
|
|
|
def test_crop(self):
|
|
def crop(mode):
|
|
im = hopper(mode)
|
|
self.assert_image_equal(im.crop(), im)
|
|
|
|
cropped = im.crop((50, 50, 100, 100))
|
|
self.assertEqual(cropped.mode, mode)
|
|
self.assertEqual(cropped.size, (50, 50))
|
|
for mode in "1", "P", "L", "RGB", "I", "F":
|
|
crop(mode)
|
|
|
|
def test_wide_crop(self):
|
|
|
|
def crop(*bbox):
|
|
i = im.crop(bbox)
|
|
h = i.histogram()
|
|
while h and not h[-1]:
|
|
del h[-1]
|
|
return tuple(h)
|
|
|
|
im = Image.new("L", (100, 100), 1)
|
|
|
|
self.assertEqual(crop(0, 0, 100, 100), (0, 10000))
|
|
self.assertEqual(crop(25, 25, 75, 75), (0, 2500))
|
|
|
|
# sides
|
|
self.assertEqual(crop(-25, 0, 25, 50), (1250, 1250))
|
|
self.assertEqual(crop(0, -25, 50, 25), (1250, 1250))
|
|
self.assertEqual(crop(75, 0, 125, 50), (1250, 1250))
|
|
self.assertEqual(crop(0, 75, 50, 125), (1250, 1250))
|
|
|
|
self.assertEqual(crop(-25, 25, 125, 75), (2500, 5000))
|
|
self.assertEqual(crop(25, -25, 75, 125), (2500, 5000))
|
|
|
|
# corners
|
|
self.assertEqual(crop(-25, -25, 25, 25), (1875, 625))
|
|
self.assertEqual(crop(75, -25, 125, 25), (1875, 625))
|
|
self.assertEqual(crop(75, 75, 125, 125), (1875, 625))
|
|
self.assertEqual(crop(-25, 75, 25, 125), (1875, 625))
|
|
|
|
def test_negative_crop(self):
|
|
# Check negative crop size (@PIL171)
|
|
|
|
im = Image.new("L", (512, 512))
|
|
im = im.crop((400, 400, 200, 200))
|
|
|
|
self.assertEqual(im.size, (0, 0))
|
|
self.assertEqual(len(im.getdata()), 0)
|
|
self.assertRaises(IndexError, lambda: im.getdata()[0])
|
|
|
|
def test_crop_float(self):
|
|
# Check cropping floats are rounded to nearest integer
|
|
# https://github.com/python-pillow/Pillow/issues/1744
|
|
|
|
# Arrange
|
|
im = Image.new("RGB", (10, 10))
|
|
self.assertEqual(im.size, (10, 10))
|
|
|
|
# Act
|
|
cropped = im.crop((0.9, 1.1, 4.2, 5.8))
|
|
|
|
# Assert
|
|
self.assertEqual(cropped.size, (3, 5))
|
|
|
|
def test_crop_crash(self):
|
|
# Image.crop crashes prepatch with an access violation
|
|
# apparently a use after free on windows, see
|
|
# https://github.com/python-pillow/Pillow/issues/1077
|
|
|
|
test_img = 'Tests/images/bmp/g/pal8-0.bmp'
|
|
extents = (1, 1, 10, 10)
|
|
# works prepatch
|
|
img = Image.open(test_img)
|
|
img2 = img.crop(extents)
|
|
img2.load()
|
|
|
|
# fail prepatch
|
|
img = Image.open(test_img)
|
|
img = img.crop(extents)
|
|
img.load()
|
|
|
|
def test_crop_zero(self):
|
|
|
|
im = Image.new('RGB', (0, 0), 'white')
|
|
|
|
cropped = im.crop((0, 0, 0, 0))
|
|
self.assertEqual(cropped.size, (0, 0))
|
|
|
|
cropped = im.crop((10, 10, 20, 20))
|
|
self.assertEqual(cropped.size, (10, 10))
|
|
self.assertEqual(cropped.getdata()[0], (0, 0, 0))
|
|
|
|
im = Image.new('RGB', (0, 0))
|
|
|
|
cropped = im.crop((10, 10, 20, 20))
|
|
self.assertEqual(cropped.size, (10, 10))
|
|
self.assertEqual(cropped.getdata()[2], (0, 0, 0))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|