Skip to content

Instantly share code, notes, and snippets.

@will-moore
Created January 14, 2016 23:01
Show Gist options
  • Save will-moore/8c6cda301020d67c802a to your computer and use it in GitHub Desktop.
Save will-moore/8c6cda301020d67c802a to your computer and use it in GitHub Desktop.
Example of how to subclass OMERO gateway to extend renderImage() as requested in http://lists.openmicroscopy.org.uk/pipermail/ome-devel/2016-January/003560.html
# Example of how to subclass ImageWrapper to extend renderImage()
# First part of this can be placed in a different python file
# As long as it is imported before being used
import omero
import omero.gateway
import PIL.ImageOps
class NewImageWrapper (omero.gateway.ImageWrapper):
# def renderImage(self, z, t, compression=0.9):
def renderImage(self, *args, **kwargs):
invert = 'invert' in kwargs and kwargs['invert']
if 'invert' in kwargs:
del kwargs['invert']
img = super(NewImageWrapper, self).renderImage(*args, **kwargs)
if invert:
img = PIL.ImageOps.invert(img)
return img
# This makes sure that conn.getObject("Image") will return
# Our NewImageWrapper subclass
omero.gateway.ImageWrapper = NewImageWrapper
omero.gateway.refreshWrappers()
# Test it, possibly in a separate file
from omero.gateway import BlitzGateway
conn = BlitzGateway('will', 'ome')
conn.connect()
image = conn.getObject("Image", 3733)
print image # <NewImageWrapper id=3732>
# without inverting
pilImg = image.renderImage(0, 0)
pilImg.show()
# with inverting
invertedImg = image.renderImage(0, 0, invert=True)
invertedImg.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment