diff --git a/examples/Training/python/ROIs.py b/examples/Training/python/ROIs.py index a2e18534024..28dc3c87e1c 100755 --- a/examples/Training/python/ROIs.py +++ b/examples/Training/python/ROIs.py @@ -12,6 +12,7 @@ """ import omero +from omero.model.enums import UnitsLength from omero.rtypes import rdouble, rint, rstring from omero.gateway import BlitzGateway from Parse_OMERO_Properties import USERNAME, PASSWORD, HOST, PORT @@ -31,8 +32,7 @@ # We are using the core Python API and omero.model objects here, since ROIs # are not yet supported in the Python Blitz Gateway. # -# In this example, we create an ROI with a rectangular shape and attach it to -# an image. +# First we load our image and pick some parameters for shapes x = 50 y = 200 width = 100 @@ -40,15 +40,32 @@ image = conn.getObject("Image", imageId) theZ = image.getSizeZ() / 2 theT = 0 -print ("Adding a rectangle at theZ: %s, theT: %s, X: %s, Y: %s, width: %s," - " height: %s" % (theZ, theT, x, y, width, height)) -# create an ROI, link it to Image -roi = omero.model.RoiI() -# use the omero.model.ImageI that underlies the 'image' wrapper -roi.setImage(image._obj) -# create a rectangle shape and add to ROI +# We have a helper function for creating an ROI and linking it to new shapes +def createROI(img, shapes): + # create an ROI, link it to Image + roi = omero.model.RoiI() + # use the omero.model.ImageI that underlies the 'image' wrapper + roi.setImage(img._obj) + for shape in shapes: + roi.addShape(shape) + # Save the ROI (saves any linked shapes too) + updateService.saveObject(roi) + + +# Another helper for generating the color integers for shapes +def rgbaToInt(red, green, blue, alpha=255): + """ Convert an R,G,B,A value to an int """ + RGBAInt = (alpha << 24) + (red << 16) + (green << 8) + blue + if (RGBAInt > (2**31-1)): # convert to signed 32-bit int + RGBAInt = RGBAInt - 2**32 + return int(RGBAInt) + + +# create a rectangle shape (added to ROI below) +print ("Adding a rectangle at theZ: %s, theT: %s, X: %s, Y: %s, width: %s," + " height: %s" % (theZ, theT, x, y, width, height)) rect = omero.model.RectangleI() rect.x = rdouble(x) rect.y = rdouble(y) @@ -57,9 +74,8 @@ rect.theZ = rint(theZ) rect.theT = rint(theT) rect.textValue = rstring("test-Rectangle") -roi.addShape(rect) -# create an Ellipse shape and add to ROI +# create an Ellipse shape (added to ROI below) ellipse = omero.model.EllipseI() ellipse.cx = rdouble(y) ellipse.cy = rdouble(x) @@ -68,9 +84,13 @@ ellipse.theZ = rint(theZ) ellipse.theT = rint(theT) ellipse.textValue = rstring("test-Ellipse") -roi.addShape(ellipse) -# create a line shape and add to ROI +# Create an ROI containing 2 shapes on same plane +# NB: OMERO.insight client doesn't support this +# The ellipse is removed later (see below) +createROI(image, [rect, ellipse]) + +# create an ROI with single line shape line = omero.model.LineI() line.x1 = rdouble(x) line.x2 = rdouble(x+width) @@ -79,21 +99,33 @@ line.theZ = rint(theZ) line.theT = rint(theT) line.textValue = rstring("test-Line") -roi.addShape(line) +createROI(image, [line]) -# create a point shape and add to ROI +# create an ROI with single point shape point = omero.model.PointI() point.cx = rdouble(x) point.cy = rdouble(y) point.theZ = rint(theZ) point.theT = rint(theT) point.textValue = rstring("test-Point") -roi.addShape(point) - - -# Save the ROI (saves any linked shapes too) -r = updateService.saveAndReturnObject(roi) - +createROI(image, [point]) + + +def pointsToString(points): + """ Returns strange format supported by Insight """ + points = ["%s,%s" % (p[0], p[1]) for p in points] + csv = ", ".join(points) + return "points[%s] points1[%s] points2[%s]" % (csv, csv, csv) +# create an ROI with a single polygon, setting colors and lineWidth +polygon = omero.model.PolygonI() +polygon.theZ = rint(theZ) +polygon.theT = rint(theT) +polygon.fillColor = rint(rgbaToInt(255, 0, 255, 50)) +polygon.strokeColor = rint(rgbaToInt(255, 255, 0)) +polygon.strokeWidth = omero.model.LengthI(10, UnitsLength.PIXEL) +points = [[10, 20], [50, 150], [200, 200], [250, 75]] +polygon.points = rstring(pointsToString(points)) +createROI(image, [polygon]) # Retrieve ROIs linked to an Image. # =================================================================