From 83c42fcabd55a378ec2a1b0d59b41a2f01f3f586 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 20 Nov 2021 10:13:10 +1100 Subject: [PATCH] Do not redeclare class each time when converting to NumPy --- src/PIL/Image.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 67365d79eb2..3b4e1e7206a 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -656,6 +656,10 @@ def _repr_png_(self): raise ValueError("Could not save to PNG for display") from e return b.getvalue() + class _ArrayData: + def __init__(self, new): + self.__array_interface__ = new + def __array__(self, dtype=None): # numpy array interface support import numpy as np @@ -672,10 +676,7 @@ def __array__(self, dtype=None): else: new["data"] = self.tobytes() - class ArrayData: - __array_interface__ = new - - return np.array(ArrayData(), dtype) + return np.array(self._ArrayData(new), dtype) def __getstate__(self): return [self.info, self.mode, self.size, self.getpalette(), self.tobytes()]