Skip to content

Commit 65bdd13

Browse files
authored
Merge pull request #394 from jo-mueller/add-repr_html
added `_repr_html_` method for `_ImageWrapper`
2 parents 2864126 + b0a7fad commit 65bdd13

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-1
lines changed

src/omero/gateway/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from omero.cmd import Chgrp2, Delete2, DoAll, SkipHead, Chown2
3030
from omero.cmd.graphs import ChildOption
3131
from omero.api import Save
32-
from omero.gateway.utils import ServiceOptsDict, GatewayConfig, toBoolean
32+
from omero.gateway.utils import ServiceOptsDict, GatewayConfig, toBoolean, image_to_html
3333
from omero.model.enums import PixelsTypeint8, PixelsTypeuint8, PixelsTypeint16
3434
from omero.model.enums import PixelsTypeuint16, PixelsTypeint32
3535
from omero.model.enums import PixelsTypeuint32, PixelsTypefloat
@@ -245,6 +245,13 @@ def __init__(self, conn=None, obj=None, cache=None, **kwargs):
245245
self._conn.SERVICE_OPTS)
246246
self.__prepare__(**kwargs)
247247

248+
def _repr_html_(self):
249+
"""
250+
Returns an HTML representation of the object. This is used by the
251+
IPython notebook to display the object in a cell.
252+
"""
253+
return image_to_html(self)
254+
248255
def __eq__(self, a):
249256
"""
250257
Returns true if the object is of the same type and has same id and name

src/omero/gateway/utils.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,111 @@ def propertiesToDict(m, prefix=None):
217217
except:
218218
d[items[-1]] = value
219219
return nested_dict
220+
221+
def image_to_html(image):
222+
import base64
223+
224+
try:
225+
pixsizeX = '{:.3f}'.format(image.getPixelSizeX())
226+
pixsizeY = '{:.3f}'.format(image.getPixelSizeY())
227+
pixsizeZ = '{:.3f}'.format(image.getPixelSizeZ())
228+
UnitX = image.getPixelSizeX().getUnit()
229+
UnitY = image.getPixelSizeY().getUnit()
230+
UnitZ = image.getPixelSizeZ().getUnit()
231+
except:
232+
pixsizeX, pixsizeY, pixsizeZ = 'na', 'na', 'na'
233+
UnitX, UnitY, UnitZ = 'na', 'na', 'na'
234+
235+
html_style_header = """
236+
<!DOCTYPE html>
237+
<html lang="en">
238+
<head>
239+
<meta charset="UTF-8">
240+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
241+
<title>Image Details</title>
242+
<style>
243+
img {
244+
min-width: 250px; /* Set the minimum width for all images */
245+
min-height: 250px; /* Set the minimum height for all images */
246+
}
247+
.align-top {
248+
vertical-align: top;
249+
}
250+
.text-right {
251+
text-align: right;
252+
}
253+
</style>
254+
</head>
255+
"""
256+
257+
def obj_html(obj, otype):
258+
return f"""<tr>
259+
<td><b>{otype}</b></td>
260+
<td class=text-right>{obj.id if obj else ""}</td>
261+
<td class=text-right>{obj.name if obj else ""}</td>
262+
</tr>
263+
"""
264+
265+
# create a sub-table for image information
266+
table_imageinfo = f"""
267+
<table>
268+
<tr><th></th><th>ID</th><th>Name</th></tr>
269+
{obj_html(image, 'Image')}
270+
{obj_html(image.getParent(), 'Dataset')}
271+
{obj_html(image.getProject(), 'Project')}
272+
</table>
273+
"""
274+
275+
# get entries for thumbnail and dimensions
276+
encoded_image = base64.b64encode(image.getThumbnail()).decode('utf-8')
277+
dimensions = f"""(
278+
{image.getSizeT()},
279+
{image.getSizeC()},
280+
{image.getSizeZ()},
281+
{image.getSizeY()},
282+
{image.getSizeX()})"""
283+
physical_dims = f"""({pixsizeZ}, {pixsizeY}, {pixsizeX})""".format()
284+
physical_units = f"""({UnitZ}, {UnitY}, {UnitX})"""
285+
286+
table_dimensions = f"""
287+
<table>\n
288+
<tr>\n
289+
<td><b>Dimensions (TCZYX): </b></td> <td class=text-right>{dimensions}</td>\n
290+
</tr>\n
291+
<tr>\n
292+
<td><b>Voxel/Pixel dimensions (ZYX): </b></td> <td class=text-right>{physical_dims}</td>\n
293+
</tr>\n
294+
<tr>\n
295+
<td><b>Physical units: </b></td> <td class=text-right>{physical_units}</td>\n
296+
</tr>\n
297+
<tr>\n
298+
<td><b>Channel Names: </b></td> <td class=text-right>{image.getChannelLabels()}</td>\n
299+
</tr>\n
300+
</table>
301+
"""
302+
303+
table_assembly = f"""
304+
<table>
305+
<tr>
306+
<td><div class="thumbnail">
307+
<img src="data:image/jpeg;base64,{encoded_image}" alt="Thumbnail">
308+
</div></td>
309+
<td class="align-top"><h2>Image information </h2>
310+
{table_imageinfo}
311+
</td>
312+
</tr>
313+
</table>
314+
<table>
315+
<tr>
316+
<td>{table_dimensions}</td>
317+
</tr>
318+
</table>
319+
"""
320+
321+
return '\n'.join([
322+
html_style_header,
323+
'<body>',
324+
table_assembly,
325+
'</body>',
326+
'</html>'
327+
])

0 commit comments

Comments
 (0)