Created
March 25, 2016 11:40
-
-
Save will-moore/51977ef420ca9f31d28a to your computer and use it in GitHub Desktop.
Python script for downloading and re-importing an image into OMERO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
from omero.gateway import BlitzGateway | |
conn = BlitzGateway("username", "passwork", port=4064, host="localhost") | |
conn.connect() | |
image = conn.getObject("Image", 54973) | |
dataset = image.getParent() | |
filename = None | |
for f in image.getImportedImageFiles(): | |
filename = f.getName() | |
t = open(f.getName(), 'wb') | |
try: | |
for chunk in f.getFileInChunks(): | |
t.write(chunk) | |
finally: | |
t.close() | |
client = conn.c | |
server = client.getProperty("omero.host") | |
port = client.getProperty("omero.port") | |
key = client.getSessionId() | |
args = [] | |
# Assume we have 'omero' on PATH | |
args.append("omero") | |
args.extend(["-s", server, "-k", key, "-p", port, "import"]) | |
# re-import into same Dataset | |
if dataset is not None: | |
args.extend(["-d", str(dataset.getId())]) | |
args.append(filename) | |
popen = subprocess.Popen(args, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
out, err = popen.communicate() | |
rc = popen.wait() | |
if rc != 0: | |
raise Exception("import failed: [%r] %s\n%s" % (args, rc, err)) | |
pix_ids = [] | |
for x in out.split("\n"): | |
try: # if the line has pixels ID... | |
pixId = str(long(x.strip())) | |
# Occasionally during tests an id is duplicated on stdout | |
if pixId not in pix_ids: | |
pix_ids.append(pixId) | |
except: | |
pass | |
print pix_ids | |
# Get Image ID from Pixels ID | |
pix = conn.getQueryService().get("Pixels", long(pix_ids[0])) | |
print pix.image.id.val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment