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

added _repr_html_ method for _ImageWrapper #394

Merged
merged 6 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 8 additions & 1 deletion src/omero/gateway/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,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
Expand Down Expand Up @@ -291,6 +291,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
Expand Down
108 changes: 108 additions & 0 deletions src/omero/gateway/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,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 = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Details</title>
<style>
img {
min-width: 250px; /* Set the minimum width for all images */
min-height: 250px; /* Set the minimum height for all images */
}
.align-top {
vertical-align: top;
}
.text-right {
text-align: right;
}
</style>
</head>
"""

def obj_html(obj, otype):
return f"""<tr>
<td><b>{otype}</b></td>
<td class=text-right>{obj.id if obj else ""}</td>
<td class=text-right>{obj.name if obj else ""}</td>
</tr>
"""

# create a sub-table for image information
table_imageinfo = f"""
<table>
<tr><th></th><th>ID</th><th>Name</th></tr>
{obj_html(image, 'Image')}
{obj_html(image.getParent(), 'Dataset')}
{obj_html(image.getProject(), 'Project')}
</table>
"""

# 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"""
<table>\n
<tr>\n
<td><b>Dimensions (TCZYX): </b></td> <td class=text-right>{dimensions}</td>\n
</tr>\n
<tr>\n
<td><b>Voxel/Pixel dimensions (ZYX): </b></td> <td class=text-right>{physical_dims}</td>\n
</tr>\n
<tr>\n
<td><b>Physical units: </b></td> <td class=text-right>{physical_units}</td>\n
</tr>\n
<tr>\n
<td><b>Channel Names: </b></td> <td class=text-right>{image.getChannelLabels()}</td>\n
</tr>\n
</table>
"""

table_assembly = f"""
<table>
<tr>
<td><div class="thumbnail">
<img src="data:image/jpeg;base64,{encoded_image}" alt="Thumbnail">
</div></td>
<td class="align-top"><h2>Image information </h2>
{table_imageinfo}
</td>
</tr>
</table>
<table>
<tr>
<td>{table_dimensions}</td>
</tr>
</table>
"""

return '\n'.join([
html_style_header,
'<body>',
table_assembly,
'</body>',
'</html>'
])