Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numpy pixels type #387

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 27 additions & 23 deletions src/omero/gateway/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -85,6 +86,16 @@

logger = logging.getLogger(__name__)
THISPATH = os.path.dirname(os.path.abspath(__file__))
OMERO_NUMPY_DTYPES = {
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:
from PIL import Image, ImageDraw, ImageFont # see ticket:2597
Expand Down Expand Up @@ -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
Expand All @@ -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 = OMERO_NUMPY_DTYPES[newPtype]
img._obj.setName(rstring(imageName))
img._obj.setSeries(rint(0))
updateService.saveObject(img._obj, self.SERVICE_OPTS)
Expand Down Expand Up @@ -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 OMERO_NUMPY_DTYPES[pixels_type]

def getTiles(self, zctTileList):
"""
Returns generator of numpy 2D planes from this set of pixels for a
Expand All @@ -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()
Expand All @@ -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:
Expand Down