From 9c223356f1bdb2144baae9b7f97a4136d7e051bc Mon Sep 17 00:00:00 2001 From: William Moore Date: Wed, 13 Dec 2023 14:51:19 +0000 Subject: [PATCH 1/2] Add pixels.get_numpy_pixels_type() --- src/omero/gateway/__init__.py | 50 +++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/src/omero/gateway/__init__.py b/src/omero/gateway/__init__.py index cc3e48aea..aeba38ac5 100644 --- a/src/omero/gateway/__init__.py +++ b/src/omero/gateway/__init__.py @@ -25,6 +25,7 @@ from past.builtins import basestring from past.utils import old_div from builtins import object +import numpy import os import warnings @@ -85,6 +86,16 @@ logger = logging.getLogger(__name__) THISPATH = os.path.dirname(os.path.abspath(__file__)) +OMERO_NUMPY_DTYPES = { + PixelsTypeint8: 'int8', + PixelsTypeuint8: 'uint8', + PixelsTypeint16: 'int16', + PixelsTypeuint16: 'uint16', + PixelsTypeint32: 'int32', + PixelsTypeuint32: 'uint32', + PixelsTypefloat: 'float32', + PixelsTypedouble: 'double' +} try: from PIL import Image, ImageDraw, ImageFont # see ticket:2597 @@ -3776,8 +3787,6 @@ def planeGen(): containerService = self.getContainerService() updateService = self.getUpdateService() - import numpy - def createImage(firstPlane, channelList): """ Create our new Image once we have the first plane in hand """ convertToType = None @@ -3792,16 +3801,8 @@ def createImage(firstPlane, channelList): # of our new image img = self.getObject("Image", iId.getValue()) newPtype = img.getPrimaryPixels().getPixelsType().getValue() - omeroToNumpy = {PixelsTypeint8: 'int8', - PixelsTypeuint8: 'uint8', - PixelsTypeint16: 'int16', - PixelsTypeuint16: 'uint16', - PixelsTypeint32: 'int32', - PixelsTypeuint32: 'uint32', - PixelsTypefloat: 'float32', - PixelsTypedouble: 'double'} - if omeroToNumpy[newPtype] != firstPlane.dtype.name: - convertToType = getattr(numpy, omeroToNumpy[newPtype]) + if OMERO_NUMPY_DTYPES[newPtype] != firstPlane.dtype.name: + convertToType = numpy.dtype(OMERO_NUMPY_DTYPES[newPtype]) img._obj.setName(rstring(imageName)) img._obj.setSeries(rint(0)) updateService.saveObject(img._obj, self.SERVICE_OPTS) @@ -7495,6 +7496,10 @@ def getPlane(self, theZ=0, theC=0, theT=0): planeList = list(self.getPlanes([(theZ, theC, theT)])) return planeList[0] + def get_numpy_pixels_type(self): + pixels_type = self.getPixelsType().value + return numpy.dtype(OMERO_NUMPY_DTYPES[pixels_type]) + def getTiles(self, zctTileList): """ Returns generator of numpy 2D planes from this set of pixels for a @@ -7504,22 +7509,21 @@ def getTiles(self, zctTileList): :param zctrList: A list of indexes: [(z,c,t, region), ] """ - import numpy from struct import unpack - pixelTypes = {PixelsTypeint8: ['b', numpy.int8], - PixelsTypeuint8: ['B', numpy.uint8], - PixelsTypeint16: ['h', numpy.int16], - PixelsTypeuint16: ['H', numpy.uint16], - PixelsTypeint32: ['i', numpy.int32], - PixelsTypeuint32: ['I', numpy.uint32], - PixelsTypefloat: ['f', numpy.float32], - PixelsTypedouble: ['d', numpy.float64]} + pixelTypes = {PixelsTypeint8: 'b', + PixelsTypeuint8: 'B', + PixelsTypeint16: 'h', + PixelsTypeuint16: 'H', + PixelsTypeint32: 'i', + PixelsTypeuint32: 'I', + PixelsTypefloat: 'f', + PixelsTypedouble: 'd'} rawPixelsStore = None sizeX = self.sizeX sizeY = self.sizeY pixelType = self.getPixelsType().value - numpyType = pixelTypes[pixelType][1] + numpyType = self.get_numpy_pixels_type() exc = None try: rawPixelsStore = self._prepareRawPixelsStore() @@ -7537,7 +7541,7 @@ def getTiles(self, zctTileList): planeX = width # +str(sizeX*sizeY)+pythonTypes[pixelType] convertType = '>%d%s' % ( - (planeY*planeX), pixelTypes[pixelType][0]) + (planeY*planeX), pixelTypes[pixelType]) if isinstance(rawPlane, bytes): convertedPlane = unpack(convertType, rawPlane) else: From 359c48b58d4e12a3fad6841f67ddd40bfcdc245a Mon Sep 17 00:00:00 2001 From: William Moore Date: Wed, 13 Dec 2023 15:13:29 +0000 Subject: [PATCH 2/2] OMERO_NUMPY_DTYPES holds types not strings --- src/omero/gateway/__init__.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/omero/gateway/__init__.py b/src/omero/gateway/__init__.py index aeba38ac5..bdb4753b6 100644 --- a/src/omero/gateway/__init__.py +++ b/src/omero/gateway/__init__.py @@ -87,14 +87,14 @@ logger = logging.getLogger(__name__) THISPATH = os.path.dirname(os.path.abspath(__file__)) OMERO_NUMPY_DTYPES = { - PixelsTypeint8: 'int8', - PixelsTypeuint8: 'uint8', - PixelsTypeint16: 'int16', - PixelsTypeuint16: 'uint16', - PixelsTypeint32: 'int32', - PixelsTypeuint32: 'uint32', - PixelsTypefloat: 'float32', - PixelsTypedouble: 'double' + PixelsTypeint8: numpy.int8, + PixelsTypeuint8: numpy.uint8, + PixelsTypeint16: numpy.int16, + PixelsTypeuint16: numpy.uint16, + PixelsTypeint32: numpy.int32, + PixelsTypeuint32: numpy.uint32, + PixelsTypefloat: numpy.float32, + PixelsTypedouble: numpy.float64, } try: @@ -3802,7 +3802,7 @@ def createImage(firstPlane, channelList): img = self.getObject("Image", iId.getValue()) newPtype = img.getPrimaryPixels().getPixelsType().getValue() if OMERO_NUMPY_DTYPES[newPtype] != firstPlane.dtype.name: - convertToType = numpy.dtype(OMERO_NUMPY_DTYPES[newPtype]) + convertToType = OMERO_NUMPY_DTYPES[newPtype] img._obj.setName(rstring(imageName)) img._obj.setSeries(rint(0)) updateService.saveObject(img._obj, self.SERVICE_OPTS) @@ -7498,7 +7498,7 @@ def getPlane(self, theZ=0, theC=0, theT=0): def get_numpy_pixels_type(self): pixels_type = self.getPixelsType().value - return numpy.dtype(OMERO_NUMPY_DTYPES[pixels_type]) + return OMERO_NUMPY_DTYPES[pixels_type] def getTiles(self, zctTileList): """