From 7d8dcf12a91a21a46ac5ac4079937f7114a6bfeb Mon Sep 17 00:00:00 2001 From: KG Date: Mon, 11 Sep 2023 20:49:39 -0400 Subject: [PATCH] Add NumPy shape attribute for pictures --- tivars/PIL/common.py | 7 +------ tivars/types/picture.py | 10 +++++++++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/tivars/PIL/common.py b/tivars/PIL/common.py index 068f9df..7391172 100644 --- a/tivars/PIL/common.py +++ b/tivars/PIL/common.py @@ -102,12 +102,7 @@ def encode(self, bufsize): """ img = self._T() - shape = img.height, img.width - - if img.pixel_type != int: - shape += 3, - - img.load_array(np.asarray(self.im).reshape(shape).tolist()) + img.load_array(np.asarray(self.im).reshape(img.np_shape).tolist()) data = img.export().bytes() return len(data), 0, data diff --git a/tivars/types/picture.py b/tivars/types/picture.py index b30998a..dc1f4d5 100644 --- a/tivars/types/picture.py +++ b/tivars/types/picture.py @@ -177,6 +177,11 @@ class PictureEntry(SizedEntry): The type of a single pixel """ + np_shape = (height, width, 3) + """ + The shape of this image as a NumPy array + """ + has_color = True """ Whether this picture has color @@ -233,10 +238,11 @@ class TIMonoPicture(PictureEntry): data_width = width // 8 data_height = height - data_offset = 0 pil_mode = "L" pixel_type = int + np_shape = (height, width) + has_color = False _type_id = 0x07 @@ -288,6 +294,7 @@ class TIPicture(PictureEntry, register=True): pil_mode = "RGB" pixel_type = RGB + np_shape = (height, width, 3) _type_id = 0x07 @@ -353,6 +360,7 @@ class TIImage(PictureEntry, register=True): pil_mode = "RGB" pixel_type = RGB + np_shape = (height, width, 3) leading_bytes = b'\x81'