Get ImageID after upload to OMERO in an Imagej python script

Hi @OMETeam ,

I would like to upload an image to our OMERO server in an ImageJ python script. For this, I have successfully used below function.

As the #TODO mentions, my question is: How could I get the ID of the newly uploaded image?

def upload_image_to_omero(path, gateway, datasetId):

    #TODO: return the imageId of the newly uploaded image

    user = gateway.getLoggedInUser()
    ctx = SecurityContext(user.getGroupId())
    sessionKey = gateway.getSessionId(user)

    config = ImportConfig()

    config.email.set("")
    config.sendFiles.set('true')
    config.sendReport.set('false')
    config.contOnError.set('false')
    config.debug.set('false')
    config.hostname.set(HOST)
    config.sessionKey.set(sessionKey)
    config.targetClass.set("omero.model.Dataset")
    config.targetId.set(datasetId)

    loci.common.DebugTools.enableLogging("DEBUG")

    store = config.createStore()
    reader = OMEROWrapper(config)

    library = ImportLibrary(store,reader)
    errorHandler = ErrorHandler(config)

    library.addObserver(LoggingImportMonitor())
    str2d = java.lang.reflect.Array.newInstance(java.lang.String,[1])
    str2d [0] = path
    candidates = ImportCandidates (reader, str2d, errorHandler)
    reader.setMetadataOptions(DefaultMetadataOptions(MetadataLevel.ALL))
    success = library.importCandidates(config, candidates)

    return success

I saw that it is actually already printed in the console (“Image:1462869”) when the function is executed, but I’d like to get it as a variable and return it afterwards.

OMERO Version: 5.4.10-ice36-b105
Bioformats version: 6.7.0-SNAPSHOT revision: 25645389e076a7bd0011e04c4dd8982c0f0614ed date: 4 April 2021
Attempting initial SSL connection to omero.biozentrum.unibas.ch:4064
Depth: 4 Metadata Level: MINIMUM
[ERROR]  *** One or more readers is misbehaving. See the debug output for more information. e.g.:
     loci.formats.in.APLReader@1d19c516 -> java.lang.NullPointerException('null') ***
OMETiffReader initializing V:\IAP-volkan-register-high-and-low-mag\DUP_15w_WT_cons_NMJ_EDL2_2.1_Tbx21-561_Chrne-647_Lam-488_02_crop1_c3.ome.tif
Reading IFDs
Populating metadata
1 file(s) parsed into 1 group(s) with 1 call(s) to setId in 1011ms. (1023ms total) [0 unknowns]
FILESET_UPLOAD_PREPARATION
FILESET_UPLOAD_START
Transferring V:\IAP-volkan-register-high-and-low-mag\DUP_15w_WT_cons_NMJ_EDL2_2.1_Tbx21-561_Chrne-647_Lam-488_02_crop1_c3.ome.tif...
FILE_UPLOAD_STARTED: V:\IAP-volkan-register-high-and-low-mag\DUP_15w_WT_cons_NMJ_EDL2_2.1_Tbx21-561_Chrne-647_Lam-488_02_crop1_c3.ome.tif
FILE_UPLOAD_COMPLETE: V:\IAP-volkan-register-high-and-low-mag\DUP_15w_WT_cons_NMJ_EDL2_2.1_Tbx21-561_Chrne-647_Lam-488_02_crop1_c3.ome.tif
FILESET_UPLOAD_END
IMPORT_STARTED Logfile: 4088132
METADATA_IMPORTED Step: 1 of 5  Logfile: 4088132
PIXELDATA_PROCESSED Step: 2 of 5  Logfile: 4088132
THUMBNAILS_GENERATED Step: 3 of 5  Logfile: 4088132
METADATA_PROCESSED Step: 4 of 5  Logfile: 4088132
OBJECTS_RETURNED Step: 5 of 5  Logfile: 4088132
IMPORT_DONE Imported file: V:\IAP-volkan-register-high-and-low-mag\DUP_15w_WT_cons_NMJ_EDL2_2.1_Tbx21-561_Chrne-647_Lam-488_02_crop1_c3.ome.tif
Image:1462869
Other imported objects:
Fileset:831032

Thank you for your help!

Hi @CellKai
In order to achieve what you want you will probably have to use the List<Pixels> importImage method.

Cheers

Jmarie

It’s probably more elaborate than the simpler solution you’re looking for, but our automated workflow for importing includes an Importer class containing a get_image_ids() method. We do it by creating a HQL query to look for Image objects with a certain clientPath. You can have a look at it here. Hopefully that is of some help!

1 Like

Hi @erickratamero and Jmarie,
thank you for your replies! I indeed hoped that it would maybe be just a simple problem with a few lines to add :sweat_smile:
So in the end I managed to alternatively work around the need for returning the imageID for the time being.
Thank you for your time!

@CellKai : :+1: If you decide to come back to this, I think a few small modifications in ImportLibrary would make this easier for future users. ~Josh.