Skip to content

Commit

Permalink
Merge pull request #1 from radarhere/ImageOps.contain-function-issue-…
Browse files Browse the repository at this point in the history
…in-finding-new-size

Round position in pad()
  • Loading branch information
bibinhashley committed Aug 30, 2022
2 parents 192aae9 + f9d3ee0 commit cf4017b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
19 changes: 19 additions & 0 deletions Tests/test_imageops.py
Expand Up @@ -110,6 +110,16 @@ def test_contain(new_size):
assert new_im.size == (256, 256)


def test_contain_round():
im = Image.new("1", (43, 63), 1)
new_im = ImageOps.contain(im, (5, 7))
assert new_im.width == 5

im = Image.new("1", (63, 43), 1)
new_im = ImageOps.contain(im, (7, 5))
assert new_im.height == 5


def test_pad():
# Same ratio
im = hopper()
Expand All @@ -130,6 +140,15 @@ def test_pad():
)


def test_pad_round():
im = Image.new("1", (1, 1), 1)
new_im = ImageOps.pad(im, (4, 1))
assert new_im.load()[2, 0] == 1

new_im = ImageOps.pad(im, (1, 4))
assert new_im.load()[0, 2] == 1


def test_pil163():
# Division by zero in equalize if < 255 pixels in image (@PIL163)

Expand Down
4 changes: 2 additions & 2 deletions src/PIL/ImageOps.py
Expand Up @@ -292,10 +292,10 @@ def pad(image, size, method=Image.Resampling.BICUBIC, color=None, centering=(0.5
else:
out = Image.new(image.mode, size, color)
if resized.width != size[0]:
x = int((size[0] - resized.width) * max(0, min(centering[0], 1)))
x = round((size[0] - resized.width) * max(0, min(centering[0], 1)))
out.paste(resized, (x, 0))
else:
y = int((size[1] - resized.height) * max(0, min(centering[1], 1)))
y = round((size[1] - resized.height) * max(0, min(centering[1], 1)))
out.paste(resized, (0, y))
return out

Expand Down

0 comments on commit cf4017b

Please sign in to comment.