Skip to content

Commit

Permalink
crop() with multi-band format
Browse files Browse the repository at this point in the history
  • Loading branch information
yoursunny committed Jun 2, 2024
1 parent 86e7fc6 commit 587bb98
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
20 changes: 15 additions & 5 deletions Tests/test_file_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,11 +877,18 @@ def test_oom(self, test_file: str) -> None:
def test_open_tiff_uint16_multiband(self):
"""Test opening multiband TIFFs and reading all channels."""

def getpixel00(im: Image.Image):
def check_pixel(im: Image.Image, expected_pixel, pos: tuple[int, int]):
actual_pixel = im.getpixel((0, 0))
if isinstance(actual_pixel, int):
actual_pixel = (actual_pixel,)
return actual_pixel
assert actual_pixel == expected_pixel

def check_image(im: Image.Image, width: int, height: int, expected_pixel):
assert im.width == width
assert im.height == height
for x in range(im.width):
for y in range(im.height):
check_pixel(im, expected_pixel, (x, y))

base_value = 4660
for i in range(1, 6):
Expand All @@ -890,10 +897,13 @@ def getpixel00(im: Image.Image):
im = Image.open(infile)

im.load()
assert getpixel00(im) == pixel
check_image(im, 10, 10, pixel)

im1 = im.copy()
check_image(im1, 10, 10, pixel)

im.copy()
assert getpixel00(im) == pixel
im2 = im.crop((2, 2, 7, 7))
check_image(im2, 5, 5, pixel)


@pytest.mark.skipif(not is_win32(), reason="Windows only")
Expand Down
3 changes: 2 additions & 1 deletion src/libImaging/Crop.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ ImagingCrop(Imaging imIn, int sx0, int sy0, int sx1, int sy1) {
ysize = 0;
}

imOut = ImagingNewDirty(imIn->mode, (ImagingNewParams){xsize, ysize});
imOut = ImagingNewDirty(
imIn->mode, (ImagingNewParams){xsize, ysize, imIn->depth, imIn->bands});
if (!imOut) {
return NULL;
}
Expand Down

0 comments on commit 587bb98

Please sign in to comment.