[ome-devel] OME python binding

Carnë Draug carandraug+dev at gmail.com
Fri Sep 26 12:48:50 BST 2014


On Thu, 25 Sep 2014 13:31:53 -0400, Yanling Liu <vrnova at gmail.com> wrote:
> Hi,
>
> I was able to use the CLI import (bin/omero import) to import
> images into OMERO. My question is that if OMERO python binding has
> similar capability? Can I write a little python script and call OMERO
> import function to upload some images to OMERO?
>
> What I mean is if there's python binding to the import function,
> rather than doing system call in python script to call bin/omero.

You can do something like the following:

    import omero
    import omero.cli

    client = omero.client (host = server_ip_or_hostname)
    client.createSession (username = "username", password = "password")

    cli = omero.cli.CLI()
    cli.loadplugins()
    cli._client = client.createClient(secure = True)
    cli.invoke(["import", path_to_image_file])

Note that you need to create a copy of the client when setting `_client`
because that session will be closed after the image import.

To find out the ID of the image you imported, you will need to get
this call STDOUT.  I have routinely [1] been doing the following (note
that this imports a single file but should be easy to adjust for
multiple files):


    cli = omero.cli.CLI()
    cli.loadplugins()
    cli._client = self.client.createClient(secure = True)
      cmd = [
      "import",
      "--debug", "ERROR",
    ]
    if self.datasetID:
      cmd.extend(["-d", str(self.datasetID)])
    if self.child_name:
      cmd.extend(["-n", self.child_name])

    cid = None
    with tempfile.NamedTemporaryFile(suffix=".stdout") as stdout:
      cmd.extend([
        "---errs", os.devnull,
        "---file", stdout.name,
      ])
      cmd.append(self.fout.name)

      STDERR = sys.stderr
      try:
        with open(os.devnull, 'w') as DEVNULL:
          sys.stderr = DEVNULL
          cli.invoke(cmd)
      finally:
        sys.stderr = STDERR
        ret_code = cli.rv
        if ret_code == 0:
          ## we only need to read one line or something is very wrong
          cid = int(stdout.readline())
          if not cid:
            raise Exception("unable to get exported image ID")
        else:
          raise Exception("failed to import processed image")

    self.child = self.conn.getObject("Image", cid)


[1] https://github.com/MicronOxford/omero_scripts_processing/blob/40e29af8/omero_scripts_processing.py#L293


More information about the ome-devel mailing list