From a04c6a27e8fc6c4c3abccfb69ce892095a908da6 Mon Sep 17 00:00:00 2001 From: Andreas Florath Date: Fri, 1 Dec 2023 16:01:23 +0100 Subject: [PATCH 1/3] Optimization of ImageStat.Stat._getcount method The new implementation uses "sum" instead of the construct "functools.reduce(operator.add, ...)". Test showed that the new function is about three times faster than the original. Also it is shorter and easier to read. Signed-off-by: Andreas Florath --- src/PIL/ImageStat.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/PIL/ImageStat.py b/src/PIL/ImageStat.py index b7ebddf06..10cb08b30 100644 --- a/src/PIL/ImageStat.py +++ b/src/PIL/ImageStat.py @@ -69,10 +69,7 @@ class Stat: def _getcount(self): """Get total number of pixels in each layer""" - v = [] - for i in range(0, len(self.h), 256): - v.append(functools.reduce(operator.add, self.h[i : i + 256])) - return v + return [sum(self.h[i: i + 256]) for i in range(0, len(self.h), 256)] def _getsum(self): """Get sum of all pixels in each layer""" From f7d40ce31cfc7ecfb166b74e45b1e5844b547b86 Mon Sep 17 00:00:00 2001 From: Andreas Florath Date: Fri, 1 Dec 2023 16:15:31 +0100 Subject: [PATCH 2/3] Removed functools and operator import which are not needed anymore Signed-off-by: Andreas Florath --- src/PIL/ImageStat.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/PIL/ImageStat.py b/src/PIL/ImageStat.py index 10cb08b30..8d7dd8615 100644 --- a/src/PIL/ImageStat.py +++ b/src/PIL/ImageStat.py @@ -21,9 +21,7 @@ # See the README file for information on usage and redistribution. # -import functools import math -import operator class Stat: From e01354a2c85c9cdb4ce5dc8a6ad3cd1c6087f16f Mon Sep 17 00:00:00 2001 From: Andreas Florath Date: Fri, 1 Dec 2023 16:19:39 +0100 Subject: [PATCH 3/3] Added space before colon Signed-off-by: Andreas Florath --- src/PIL/ImageStat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PIL/ImageStat.py b/src/PIL/ImageStat.py index 8d7dd8615..c0f647024 100644 --- a/src/PIL/ImageStat.py +++ b/src/PIL/ImageStat.py @@ -67,7 +67,7 @@ class Stat: def _getcount(self): """Get total number of pixels in each layer""" - return [sum(self.h[i: i + 256]) for i in range(0, len(self.h), 256)] + return [sum(self.h[i : i + 256]) for i in range(0, len(self.h), 256)] def _getsum(self): """Get sum of all pixels in each layer"""