From ab6c60da1c761456d9b449d6e1bf9efd0f0947c2 Mon Sep 17 00:00:00 2001 From: "neiljp (Neil Pilgrim)" Date: Sat, 19 Aug 2017 13:12:51 -0700 Subject: [PATCH 1/6] Image.merge: Rename loop variable for clarity and type-safety. --- PIL/Image.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index e1c6f4928..056474ddf 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -2641,10 +2641,10 @@ def merge(mode, bands): if getmodebands(mode) != len(bands) or "*" in mode: raise ValueError("wrong number of bands") - for im in bands[1:]: - if im.mode != getmodetype(mode): + for band in bands[1:]: + if band.mode != getmodetype(mode): raise ValueError("mode mismatch") - if im.size != bands[0].size: + if band.size != bands[0].size: raise ValueError("size mismatch") for band in bands: band.load() From 72b3e6819d9256ee2bf60c6d7504dc2406e6a3d6 Mon Sep 17 00:00:00 2001 From: "neiljp (Neil Pilgrim)" Date: Sat, 19 Aug 2017 22:18:55 -0700 Subject: [PATCH 2/6] Image._dump: Rewrite for clarity and type-safety. --- PIL/Image.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index 056474ddf..96bd66b2e 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -583,21 +583,27 @@ class Image(object): def _dump(self, file=None, format=None, **options): import tempfile + suffix = '' if format: suffix = '.'+format + if not file: - f, file = tempfile.mkstemp(suffix) + f, filename = tempfile.mkstemp(suffix) os.close(f) + else: + filename = file + if not filename.endswith(suffix): + filename = filename + suffix self.load() + if not format or format == "PPM": - self.im.save_ppm(file) + self.im.save_ppm(filename) else: - if not file.endswith(format): - file = file + "." + format - self.save(file, format, **options) - return file + self.save(filename, format, **options) + + return filename def __eq__(self, other): return (self.__class__.__name__ == other.__class__.__name__ and From 6f69379fdcd3f095cb3ab8a73d8079e6e7554df5 Mon Sep 17 00:00:00 2001 From: "neiljp (Neil Pilgrim)" Date: Sun, 20 Aug 2017 18:39:40 -0700 Subject: [PATCH 3/6] Image.remap_palette: Tweak to improve typing. --- PIL/Image.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index 96bd66b2e..c94c8b47e 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -1624,16 +1624,18 @@ class Image(object): if source_palette is None: if self.mode == "P": - source_palette = self.im.getpalette("RGB")[:768] + real_source_palette = self.im.getpalette("RGB")[:768] else: # L-mode - source_palette = bytearray(i//3 for i in range(768)) + real_source_palette = bytearray(i//3 for i in range(768)) + else: + real_source_palette = source_palette palette_bytes = b"" new_positions = [0]*256 # pick only the used colors from the palette for i, oldPosition in enumerate(dest_map): - palette_bytes += source_palette[oldPosition*3:oldPosition*3+3] + palette_bytes += real_source_palette[oldPosition*3:oldPosition*3+3] new_positions[oldPosition] = i # replace the palette color id of all pixel with the new id From 470da1c6de0c8a2eb297a56388c02d013a098da3 Mon Sep 17 00:00:00 2001 From: "neiljp (Neil Pilgrim)" Date: Sun, 20 Aug 2017 19:22:51 -0700 Subject: [PATCH 4/6] Image.rotate: Adjust variable use to avoid None & add comment re int/float. --- PIL/Image.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index c94c8b47e..f826588c5 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -1791,9 +1791,13 @@ class Image(object): w, h = self.size if translate is None: - translate = [0, 0] + post_trans = (0, 0) + else: + post_trans = translate if center is None: - center = [w / 2.0, h / 2.0] + rotn_center = (w / 2.0, h / 2.0) # FIXME These should be rounded to ints? + else: + rotn_center = center angle = - math.radians(angle) matrix = [ @@ -1805,10 +1809,10 @@ class Image(object): (a, b, c, d, e, f) = matrix return a*x + b*y + c, d*x + e*y + f - matrix[2], matrix[5] = transform(-center[0] - translate[0], - -center[1] - translate[1], matrix) - matrix[2] += center[0] - matrix[5] += center[1] + matrix[2], matrix[5] = transform(-rotn_center[0] - post_trans[0], + -rotn_center[1] - post_trans[1], matrix) + matrix[2] += rotn_center[0] + matrix[5] += rotn_center[1] if expand: # calculate output size From 58df924da6e17da54d0cacad615df4bea8823135 Mon Sep 17 00:00:00 2001 From: "neiljp (Neil Pilgrim)" Date: Tue, 22 Aug 2017 14:38:41 -0700 Subject: [PATCH 5/6] Image.py: _conv_type_shape: refactor for clarity and type-safety. --- PIL/Image.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index f826588c5..a45b9bf70 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -270,12 +270,11 @@ _MODE_CONV = { def _conv_type_shape(im): - shape = im.size[1], im.size[0] typ, extra = _MODE_CONV[im.mode] if extra is None: - return shape, typ + return (im.size[1], im.size[0]), typ else: - return shape+(extra,), typ + return (im.size[1], im.size[0], extra), typ MODES = sorted(_MODEINFO) From 9ec023503d5a521125d419def7c296826fc9e69a Mon Sep 17 00:00:00 2001 From: "neiljp (Neil Pilgrim)" Date: Tue, 22 Aug 2017 14:39:00 -0700 Subject: [PATCH 6/6] Image.__eq__: Add isinstance check. --- PIL/Image.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/PIL/Image.py b/PIL/Image.py index a45b9bf70..7e51cc832 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -605,7 +605,8 @@ class Image(object): return filename def __eq__(self, other): - return (self.__class__.__name__ == other.__class__.__name__ and + return (isinstance(other, Image) and + self.__class__.__name__ == other.__class__.__name__ and self.mode == other.mode and self.size == other.size and self.info == other.info and