Setting pixel sizes of an image

Setting pixel sizes of an image#

A key part of the metadata are the pixel/voxel sizes of image data. When upload processed image data to OMERO, they are by default not part of the uploaded data but a key part you wish to add to your data. Following up this dicsussion on image.sc, here’s how you can do it using omero-py:

import omero
import ezomero

from omero.model.enums import UnitsLength
host = 'omero-int.biotec.tu-dresden.de'
user = ''  # replace this with your username
secure = True
port = 4064
group = 'default'

conn = ezomero.connect(host=host, user=user, secure=secure, port=port, group=group)

We first need to retrieve the remote object:

image_object = conn.getObject("Image", 330)

If there are already pixel sizes set, you can retrieve them like this:

print('Pixel size X: ', image_object.getPixelSizeX())
print('Pixel size Y: ', image_object.getPixelSizeY())
print('Pixel size Z: ', image_object.getPixelSizeZ())
Pixel size X:  0.2
Pixel size Y:  0.2
Pixel size Z:  2.0

If you want to set them yourself, you can do it like this:

unit_x = omero.model.LengthI(0.5, UnitsLength.MICROMETER)
unit_y = omero.model.LengthI(0.5, UnitsLength.MICROMETER)
unit_z = omero.model.LengthI(3, UnitsLength.MICROMETER)

primary_pixels_object = image_object.getPrimaryPixels()._obj
primary_pixels_object.setPhysicalSizeX(unit_x)
primary_pixels_object.setPhysicalSizeY(unit_y)
primary_pixels_object.setPhysicalSizeZ(unit_z)

# update the image object
conn.getUpdateService().saveObject(primary_pixels_object)

We can doublecheck whether this worked:

image_object = conn.getObject("Image", 330)

print('Pixel size X: ', image_object.getPixelSizeX())
print('Pixel size Y: ', image_object.getPixelSizeY())
print('Pixel size Z: ', image_object.getPixelSizeZ())
Pixel size X:  0.5
Pixel size Y:  0.5
Pixel size Z:  3.0