From 651432b723ada2352e2ff8194cab748c3ca2b4af Mon Sep 17 00:00:00 2001 From: thodnev Date: Thu, 25 Aug 2016 02:59:20 +0300 Subject: [PATCH 1/2] Changed Image.thumbnail method's size calculation now it's faster and more pythonic --- PIL/Image.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index 839700a0f..851ef0b9c 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -1785,18 +1785,14 @@ class Image(object): :returns: None """ - # preserve aspect ratio - x, y = self.size - if x > size[0]: - y = int(max(y * size[0] / x, 1)) - x = int(size[0]) - if y > size[1]: - x = int(max(x * size[1] / y, 1)) - y = int(size[1]) - size = x, y - if size == self.size: return + # preserve aspect ratio + x, y = self.size + if x > size[0] or y > size[1]: + factor = max(x/size[0], y/size[1]) + x, y = int(x/factor), int(y/factor) + size = x, y self.draft(None, size) From b568220ae42825529b7a206a4ec8ce0bf0917e85 Mon Sep 17 00:00:00 2001 From: thodnev Date: Thu, 25 Aug 2016 03:14:42 +0300 Subject: [PATCH 2/2] Update Image.py --- PIL/Image.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index 851ef0b9c..c3e417520 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -1790,8 +1790,8 @@ class Image(object): # preserve aspect ratio x, y = self.size if x > size[0] or y > size[1]: - factor = max(x/size[0], y/size[1]) - x, y = int(x/factor), int(y/factor) + factor = min(float(size[0])/x, float(size[1])/y) + x, y = int(x*factor), int(y*factor) size = x, y self.draft(None, size)