diff --git a/src/omero/gateway/__init__.py b/src/omero/gateway/__init__.py index 6ba480d5d..9bbd2298f 100644 --- a/src/omero/gateway/__init__.py +++ b/src/omero/gateway/__init__.py @@ -29,7 +29,7 @@ from omero.cmd import Chgrp2, Delete2, DoAll, SkipHead, Chown2 from omero.cmd.graphs import ChildOption from omero.api import Save -from omero.gateway.utils import ServiceOptsDict, GatewayConfig, toBoolean +from omero.gateway.utils import ServiceOptsDict, GatewayConfig, toBoolean, image_to_html from omero.model.enums import PixelsTypeint8, PixelsTypeuint8, PixelsTypeint16 from omero.model.enums import PixelsTypeuint16, PixelsTypeint32 from omero.model.enums import PixelsTypeuint32, PixelsTypefloat @@ -245,6 +245,13 @@ def __init__(self, conn=None, obj=None, cache=None, **kwargs): self._conn.SERVICE_OPTS) self.__prepare__(**kwargs) + def _repr_html_(self): + """ + Returns an HTML representation of the object. This is used by the + IPython notebook to display the object in a cell. + """ + return image_to_html(self) + def __eq__(self, a): """ Returns true if the object is of the same type and has same id and name diff --git a/src/omero/gateway/utils.py b/src/omero/gateway/utils.py index b22c735a2..a1e33abb9 100644 --- a/src/omero/gateway/utils.py +++ b/src/omero/gateway/utils.py @@ -217,3 +217,111 @@ def propertiesToDict(m, prefix=None): except: d[items[-1]] = value return nested_dict + +def image_to_html(image): + import base64 + + try: + pixsizeX = '{:.3f}'.format(image.getPixelSizeX()) + pixsizeY = '{:.3f}'.format(image.getPixelSizeY()) + pixsizeZ = '{:.3f}'.format(image.getPixelSizeZ()) + UnitX = image.getPixelSizeX().getUnit() + UnitY = image.getPixelSizeY().getUnit() + UnitZ = image.getPixelSizeZ().getUnit() + except: + pixsizeX, pixsizeY, pixsizeZ = 'na', 'na', 'na' + UnitX, UnitY, UnitZ = 'na', 'na', 'na' + + html_style_header = """ + + + + + + Image Details + + + """ + + def obj_html(obj, otype): + return f""" + {otype} + {obj.id if obj else ""} + {obj.name if obj else ""} + + """ + + # create a sub-table for image information + table_imageinfo = f""" + + + {obj_html(image, 'Image')} + {obj_html(image.getParent(), 'Dataset')} + {obj_html(image.getProject(), 'Project')} +
IDName
+ """ + + # get entries for thumbnail and dimensions + encoded_image = base64.b64encode(image.getThumbnail()).decode('utf-8') + dimensions = f"""( + {image.getSizeT()}, + {image.getSizeC()}, + {image.getSizeZ()}, + {image.getSizeY()}, + {image.getSizeX()})""" + physical_dims = f"""({pixsizeZ}, {pixsizeY}, {pixsizeX})""".format() + physical_units = f"""({UnitZ}, {UnitY}, {UnitX})""" + + table_dimensions = f""" + \n + \n + \n + \n + \n + \n + \n + \n + \n + \n + \n + \n + \n +
Dimensions (TCZYX): {dimensions}
Voxel/Pixel dimensions (ZYX): {physical_dims}
Physical units: {physical_units}
Channel Names: {image.getChannelLabels()}
+ """ + + table_assembly = f""" + + + + + +
+ Thumbnail +

Image information

+ {table_imageinfo} +
+ + + + +
{table_dimensions}
+ """ + + return '\n'.join([ + html_style_header, + '', + table_assembly, + '', + '' + ]) \ No newline at end of file