From 08d0ec1e9386abf32806b2e63a690cdccecd3a6d Mon Sep 17 00:00:00 2001 From: Chris Bailey Date: Mon, 1 Jul 2013 12:36:46 +0100 Subject: [PATCH] Generalizing pterk's ZeroDivisionError fix for 1px images --- PIL/ImageOps.py | 14 ++++++-------- Tests/test_imageops.py | 12 +++++++++++- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/PIL/ImageOps.py b/PIL/ImageOps.py index a7f360259..943c6cafb 100644 --- a/PIL/ImageOps.py +++ b/PIL/ImageOps.py @@ -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]) diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py index 5ef7f257a..8ed5ccefa 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -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)