Skip to content

Instantly share code, notes, and snippets.

@Yu-AnChen
Created October 12, 2021 00:46
Show Gist options
  • Save Yu-AnChen/a7fb74d1a30f288b81114941ad56874a to your computer and use it in GitHub Desktop.
Save Yu-AnChen/a7fb74d1a30f288b81114941ad56874a to your computer and use it in GitHub Desktop.
Fetch pixel data of an image at specific resolution (level) from omero server
# ref https://forum.image.sc/t/omero-py-how-to-get-tiles-at-different-zoom-level-pyramidal-image/
import numpy as np
from omero.gateway import BlitzGateway
from omero.model import enums as omero_enums
import omero
def connect_hms_omero(session_id):
SESSION_ID = session_id
HOST = 'omero-app.hms.harvard.edu'
conn = BlitzGateway(SESSION_ID, SESSION_ID, host=HOST, port=4064)
conn.connect()
return conn
def connect_idr():
autocfg = [
"--Ice.Default.Router=OMERO.Glacier2/router:wss -p 443 -h @omero.host@ -r /omero-ws",
"--omero.user=public",
"--omero.pass=public"
]
HOST = 'idr.openmicroscopy.org'
c = omero.client(host=HOST, args=autocfg)
c.createSession()
c.enableKeepAlive(300)
return BlitzGateway(client_obj=c)
conn = connect_hms_omero('4819a822-2416-4cf5-a6df-d43bf1752045')
PIXEL_TYPES = {
omero_enums.PixelsTypeint8: np.int8,
omero_enums.PixelsTypeuint8: np.uint8,
omero_enums.PixelsTypeint16: np.int16,
omero_enums.PixelsTypeuint16: np.uint16,
omero_enums.PixelsTypeint32: np.int32,
omero_enums.PixelsTypeuint32: np.uint32,
omero_enums.PixelsTypefloat: np.float32,
omero_enums.PixelsTypedouble: np.float64,
}
IMAGE_ID = 1362653
# IMAGE_ID =9826019
image = conn.getObject("image", IMAGE_ID)
print('image', image.name)
pixels = image.getPrimaryPixels()
dtype = PIXEL_TYPES.get(pixels.getPixelsType().value, None)
pix = image._conn.c.sf.createRawPixelsStore()
pid = image.getPixelsId()
# level 0 is the highest level of the pyramid
level = 3
z, c, t, x, y = (0, 0, 0, 0, 0)
try:
pix.setPixelsId(pid, False)
# Get info on number of levels and sizes in (W, H)
level_sizes = [(r.sizeX, r.sizeY) for r in pix.getResolutionDescriptions()][::-1]
print(level_sizes)
pix.setResolutionLevel(level)
print(pix.getTileSize())
w, h = level_sizes[level]
# Maximum allowed is 256000000 bytes (see Ice.MessageSizeMax)
tile = pix.getTile(z, c, t, x, y, w, h)
finally:
pix.close()
tile = np.frombuffer(tile, dtype=np.dtype(dtype).newbyteorder('>'))
tile = tile.reshape((h, w))
import tifffile
tifffile.imsave('omero_test.tif', tile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment