Generalizing pterk's ZeroDivisionError fix for 1px images

This commit is contained in:
Chris Bailey 2013-07-01 12:36:46 +01:00
parent ae9b6b3209
commit 08d0ec1e93
2 changed files with 17 additions and 9 deletions

View File

@ -275,10 +275,6 @@ def fit(image, size, method=Image.NEAREST, bleed=0.0, centering=(0.5, 0.5)):
# kevin@cazabon.com
# http://www.cazabon.com
# No cropping/fit possible. Prevents ZeroDivisionError @ liveAreaAspectRatio
if image.size == (1,1):
return image
# ensure inputs are valid
if not isinstance(centering, list):
centering = [centering[0], centering[1]]
@ -300,10 +296,12 @@ def fit(image, size, method=Image.NEAREST, bleed=0.0, centering=(0.5, 0.5)):
int((float(bleed) * float(image.size[1])) + 0.5)
)
liveArea = (
bleedPixels[0], bleedPixels[1], image.size[0] - bleedPixels[0] - 1,
image.size[1] - bleedPixels[1] - 1
)
liveArea = (0, 0, image.size[0], image.size[1])
if bleed > 0.0:
liveArea = (
bleedPixels[0], bleedPixels[1], image.size[0] - bleedPixels[0] - 1,
image.size[1] - bleedPixels[1] - 1
)
liveSize = (liveArea[2] - liveArea[0], liveArea[3] - liveArea[1])

View File

@ -37,7 +37,6 @@ def test_sanity():
ImageOps.fit(lena("L"), (128, 128))
ImageOps.fit(lena("RGB"), (128, 128))
ImageOps.fit(lena("RGB").resize((1,1)), (35,35))
ImageOps.flip(lena("L"))
ImageOps.flip(lena("RGB"))
@ -59,6 +58,17 @@ def test_sanity():
success()
def test_1pxfit():
# Division by zero in equalize if image is 1 pixel high
newimg = ImageOps.fit(lena("RGB").resize((1,1)), (35,35))
assert_equal(newimg.size,(35,35))
newimg = ImageOps.fit(lena("RGB").resize((1,100)), (35,35))
assert_equal(newimg.size,(35,35))
newimg = ImageOps.fit(lena("RGB").resize((100,1)), (35,35))
assert_equal(newimg.size,(35,35))
def test_pil163():
# Division by zero in equalize if < 255 pixels in image (@PIL163)