From 23a51a35e999594ecf01389b63a08deee79b5b3a Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 22 Jun 2017 20:24:21 +1000 Subject: [PATCH 1/4] Avoid potentially loading an image twice --- PIL/Image.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index 2904e4e07..177268518 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -875,8 +875,8 @@ class Image(object): mode = "RGB" else: return self.copy() - - self.load() + else: + self.load() if matrix: # matrix conversion @@ -1062,10 +1062,10 @@ class Image(object): :returns: An :py:class:`~PIL.Image.Image` object. """ - self.load() if box is None: return self.copy() + self.load() return self._new(self._crop(self.im, box)) def _crop(self, im, box): @@ -1390,9 +1390,10 @@ class Image(object): im = im.convert(self.mode) im = im.im - self.load() if self.readonly: self._copy() + else: + self.load() if mask: mask.load() @@ -1498,9 +1499,10 @@ class Image(object): other color value. """ - self.load() if self.readonly: self._copy() + else: + self.load() if self.mode not in ("LA", "RGBA"): # attempt to promote self to a matching alpha mode @@ -1556,9 +1558,10 @@ class Image(object): :param offset: An optional offset value. The default is 0.0. """ - self.load() if self.readonly: self._copy() + else: + self.load() self.im.putdata(data, scale, offset) @@ -1612,10 +1615,9 @@ class Image(object): :param value: The pixel value. """ - self.load() if self.readonly: self._copy() - self.load() + self.load() if self.pyaccess: return self.pyaccess.putpixel(xy, value) From 411765a69c19fe648feb53db78e86ec888825a57 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 6 Sep 2017 14:22:22 +1000 Subject: [PATCH 2/4] Added _mutable method for common code --- PIL/Image.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index 177268518..9ee8fa66a 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -592,6 +592,12 @@ class Image(object): self.pyaccess = None self.readonly = 0 + def _mutable(self): + if self.readonly: + self._copy() + else: + self.load() + def _dump(self, file=None, format=None, **options): import tempfile @@ -1390,10 +1396,7 @@ class Image(object): im = im.convert(self.mode) im = im.im - if self.readonly: - self._copy() - else: - self.load() + self._mutable() if mask: mask.load() @@ -1499,10 +1502,7 @@ class Image(object): other color value. """ - if self.readonly: - self._copy() - else: - self.load() + self._mutable() if self.mode not in ("LA", "RGBA"): # attempt to promote self to a matching alpha mode @@ -1558,10 +1558,7 @@ class Image(object): :param offset: An optional offset value. The default is 0.0. """ - if self.readonly: - self._copy() - else: - self.load() + self._mutable() self.im.putdata(data, scale, offset) From d8c088ed8ba1ac1bfc150c63215e42b82ac13853 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 6 Sep 2017 14:49:15 +1000 Subject: [PATCH 3/4] If image is already in the destination mode and there is no matrix, do not convert --- PIL/Image.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index 9ee8fa66a..92cac7c9a 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -871,18 +871,16 @@ class Image(object): :returns: An :py:class:`~PIL.Image.Image` object. """ - if not mode: + self.load() + + if not mode and self.mode == "P": # determine default mode - if self.mode == "P": - self.load() - if self.palette: - mode = self.palette.mode - else: - mode = "RGB" + if self.palette: + mode = self.palette.mode else: - return self.copy() - else: - self.load() + mode = "RGB" + if not mode or (mode == self.mode and not matrix): + return self.copy() if matrix: # matrix conversion From a491500257e75501b9c20ca9208eb78756677104 Mon Sep 17 00:00:00 2001 From: Eric Soroos Date: Fri, 29 Sep 2017 08:39:56 +0000 Subject: [PATCH 4/4] Rename _mutable to _ensure_mutable --- PIL/Image.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index 92cac7c9a..cfdd35e1d 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -592,7 +592,7 @@ class Image(object): self.pyaccess = None self.readonly = 0 - def _mutable(self): + def _ensure_mutable(self): if self.readonly: self._copy() else: @@ -1394,7 +1394,7 @@ class Image(object): im = im.convert(self.mode) im = im.im - self._mutable() + self._ensure_mutable() if mask: mask.load() @@ -1500,7 +1500,7 @@ class Image(object): other color value. """ - self._mutable() + self._ensure_mutable() if self.mode not in ("LA", "RGBA"): # attempt to promote self to a matching alpha mode @@ -1556,7 +1556,7 @@ class Image(object): :param offset: An optional offset value. The default is 0.0. """ - self._mutable() + self._ensure_mutable() self.im.putdata(data, scale, offset)