Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Tests/test_imageops.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ def test_sanity() -> None:
ImageOps.exif_transpose(hopper("RGB"))


@pytest.mark.parametrize(
"border, expected_box",
(
(1, (1, 1, 127, 127)),
((1, 2), (1, 2, 127, 126)),
((1, 2, 3, 4), (1, 2, 125, 124)),
),
)
def test_crop(
border: int | tuple[int, ...], expected_box: tuple[int, int, int, int]
) -> None:
im = hopper()
assert_image_equal(ImageOps.crop(im, border), im.crop(expected_box))


def test_1pxfit() -> None:
# Division by zero in equalize if image is 1 pixel high
newimg = ImageOps.fit(hopper("RGB").resize((1, 1)), (35, 35))
Expand Down
16 changes: 11 additions & 5 deletions src/PIL/ImageOps.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,15 +400,18 @@ def pad(
return out


def crop(image: Image.Image, border: int = 0) -> Image.Image:
def crop(image: Image.Image, border: int | tuple[int, ...] = 0) -> Image.Image:
"""
Remove border from image. The same amount of pixels are removed
from all four sides. This function works on all image modes.
Remove border from image. This function works on all image modes.

.. seealso:: :py:meth:`~PIL.Image.Image.crop`

:param image: The image to crop.
:param border: The number of pixels to remove.
:param border: The number of pixels to remove. An integer removes the same
number of pixels from all four sides. A 2-tuple specifies
the number of pixels to remove horizontally and vertically.
A 4-tuple specifies the number of pixels to remove from the
left, top, right and bottom of the image.
:return: An image.
"""
left, top, right, bottom = _border(border)
Expand Down Expand Up @@ -516,7 +519,10 @@ def expand(
Add border to the image

:param image: The image to expand.
:param border: Border width, in pixels.
:param border: Border width, in pixels. An integer adds the same width to
all four sides. A 2-tuple specifies the horizontal and
vertical border widths. A 4-tuple specifies the widths of
the left, top, right and bottom borders.
:param fill: Pixel fill value (a color value). Default is 0 (black).
:return: An image.
"""
Expand Down