2020-02-12 19:29:19 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
from PIL import Image
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
from .helper import assert_image_equal, hopper
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_crop():
|
|
|
|
def crop(mode):
|
|
|
|
im = hopper(mode)
|
|
|
|
assert_image_equal(im.crop(), im)
|
2017-01-03 05:30:09 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
cropped = im.crop((50, 50, 100, 100))
|
|
|
|
assert cropped.mode == mode
|
|
|
|
assert cropped.size == (50, 50)
|
2019-06-13 18:54:24 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
for mode in "1", "P", "L", "RGB", "I", "F":
|
|
|
|
crop(mode)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_wide_crop():
|
|
|
|
def crop(*bbox):
|
|
|
|
i = im.crop(bbox)
|
|
|
|
h = i.histogram()
|
|
|
|
while h and not h[-1]:
|
|
|
|
del h[-1]
|
|
|
|
return tuple(h)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
im = Image.new("L", (100, 100), 1)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
assert crop(0, 0, 100, 100) == (0, 10000)
|
|
|
|
assert crop(25, 25, 75, 75) == (0, 2500)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# sides
|
|
|
|
assert crop(-25, 0, 25, 50) == (1250, 1250)
|
|
|
|
assert crop(0, -25, 50, 25) == (1250, 1250)
|
|
|
|
assert crop(75, 0, 125, 50) == (1250, 1250)
|
|
|
|
assert crop(0, 75, 50, 125) == (1250, 1250)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
assert crop(-25, 25, 125, 75) == (2500, 5000)
|
|
|
|
assert crop(25, -25, 75, 125) == (2500, 5000)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# corners
|
|
|
|
assert crop(-25, -25, 25, 25) == (1875, 625)
|
|
|
|
assert crop(75, -25, 125, 25) == (1875, 625)
|
|
|
|
assert crop(75, 75, 125, 125) == (1875, 625)
|
|
|
|
assert crop(-25, 75, 25, 125) == (1875, 625)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_negative_crop():
|
|
|
|
# Check negative crop size (@PIL171)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
im = Image.new("L", (512, 512))
|
|
|
|
im = im.crop((400, 400, 200, 200))
|
2016-02-24 16:06:28 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
assert im.size == (0, 0)
|
|
|
|
assert len(im.getdata()) == 0
|
|
|
|
with pytest.raises(IndexError):
|
|
|
|
im.getdata()[0]
|
2016-02-24 16:06:28 +03:00
|
|
|
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_crop_float():
|
|
|
|
# Check cropping floats are rounded to nearest integer
|
|
|
|
# https://github.com/python-pillow/Pillow/issues/1744
|
2016-02-24 16:06:28 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (10, 10))
|
|
|
|
assert im.size == (10, 10)
|
2017-01-03 05:30:09 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
|
|
|
cropped = im.crop((0.9, 1.1, 4.2, 5.8))
|
2017-01-03 05:30:09 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
|
|
|
assert cropped.size == (3, 5)
|
2016-09-29 23:28:24 +03:00
|
|
|
|
2017-01-03 05:30:09 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_crop_crash():
|
|
|
|
# Image.crop crashes prepatch with an access violation
|
|
|
|
# apparently a use after free on Windows, see
|
|
|
|
# https://github.com/python-pillow/Pillow/issues/1077
|
2017-01-03 05:30:09 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
test_img = "Tests/images/bmp/g/pal8-0.bmp"
|
|
|
|
extents = (1, 1, 10, 10)
|
|
|
|
# works prepatch
|
|
|
|
with Image.open(test_img) as img:
|
|
|
|
img2 = img.crop(extents)
|
|
|
|
img2.load()
|
2016-12-27 15:53:23 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# fail prepatch
|
|
|
|
with Image.open(test_img) as img:
|
|
|
|
img = img.crop(extents)
|
|
|
|
img.load()
|
2016-12-27 15:53:23 +03:00
|
|
|
|
2017-01-03 05:30:09 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_crop_zero():
|
|
|
|
|
|
|
|
im = Image.new("RGB", (0, 0), "white")
|
|
|
|
|
|
|
|
cropped = im.crop((0, 0, 0, 0))
|
|
|
|
assert cropped.size == (0, 0)
|
|
|
|
|
|
|
|
cropped = im.crop((10, 10, 20, 20))
|
|
|
|
assert cropped.size == (10, 10)
|
|
|
|
assert cropped.getdata()[0] == (0, 0, 0)
|
|
|
|
|
|
|
|
im = Image.new("RGB", (0, 0))
|
|
|
|
|
|
|
|
cropped = im.crop((10, 10, 20, 20))
|
|
|
|
assert cropped.size == (10, 10)
|
|
|
|
assert cropped.getdata()[2] == (0, 0, 0)
|