mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-08 22:03:09 +03:00
Improve rounding
This commit is contained in:
parent
64c08f4dba
commit
8f21d0ddf0
|
@ -58,6 +58,10 @@ def test_aspect():
|
||||||
im.thumbnail((50, 50))
|
im.thumbnail((50, 50))
|
||||||
assert im.size == (34, 50) # ratio is 0.68
|
assert im.size == (34, 50) # ratio is 0.68
|
||||||
|
|
||||||
|
im = Image.new("L", (100, 30)) # ratio is 3.333333333333
|
||||||
|
im.thumbnail((75, 75))
|
||||||
|
assert im.size == (75, 23) # ratio is 3.260869565217
|
||||||
|
|
||||||
|
|
||||||
def test_float():
|
def test_float():
|
||||||
im = Image.new("L", (128, 128))
|
im = Image.new("L", (128, 128))
|
||||||
|
|
|
@ -2240,13 +2240,20 @@ class Image:
|
||||||
if x >= self.width and y >= self.height:
|
if x >= self.width and y >= self.height:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
def round_aspect(number):
|
||||||
|
if x / y >= aspect:
|
||||||
|
key = lambda n: abs(aspect - n / y) # noqa: E731
|
||||||
|
else:
|
||||||
|
key = lambda n: abs(aspect - x / n) # noqa: E731
|
||||||
|
return max(min(math.floor(number), math.ceil(number), key=key), 1)
|
||||||
|
|
||||||
# preserve aspect ratio
|
# preserve aspect ratio
|
||||||
aspect = self.width / self.height
|
aspect = self.width / self.height
|
||||||
if x / y >= aspect:
|
if x / y >= aspect:
|
||||||
x = max(y * aspect, 1)
|
x = round_aspect(y * aspect)
|
||||||
else:
|
else:
|
||||||
y = max(x / aspect, 1)
|
y = round_aspect(x / aspect)
|
||||||
size = (round(x), round(y))
|
size = (x, y)
|
||||||
|
|
||||||
box = None
|
box = None
|
||||||
if reducing_gap is not None:
|
if reducing_gap is not None:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user