Do not redeclare class each time when converting to NumPy

This commit is contained in:
Andrew Murray 2021-11-20 10:13:10 +11:00
parent e1eefe4486
commit 83c42fcabd

View File

@ -656,6 +656,10 @@ class Image:
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 @@ class Image:
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()]