Skip to content

Instantly share code, notes, and snippets.

@will-moore
Last active December 20, 2021 10:27
Show Gist options
  • Save will-moore/6bf218bb2e7b59cb7d3c20e8ef55a0cf to your computer and use it in GitHub Desktop.
Save will-moore/6bf218bb2e7b59cb7d3c20e8ef55a0cf to your computer and use it in GitHub Desktop.
OMERO.server script to rename Images e.g. myimage_s1.tif to myimage_s001.tif to fix ordering in the clients.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# See https://forum.image.sc/t/omero-screens-data-import/33466/10
# and https://forum.image.sc/t/upload-issue-with-metadata-ome-xml-companion-file/59290/9
import omero.scripts as scripts
from omero.gateway import BlitzGateway
import omero
from omero.rtypes import rstring, rlong
import re
def pad_zero(match):
text = match.group(0)
num = match.group('num')
padded = "%03d" % int(num)
return text.replace(num, padded)
def rename_images(conn, script_params):
ids = script_params['IDs']
prefix = script_params['Number_Prefix']
p = re.compile(r'%s(?P<num>[0-9]+)' % prefix)
images = []
for dataset in conn.getObjects('Dataset', ids):
images.extend(list(dataset.listChildren()))
for image in images:
name = image.getName()
new_name = p.sub(pad_zero, name)
image.setName(new_name)
image.save()
def run_script():
data_types = [rstring('Dataset')]
client = scripts.client(
'Rename_Images.py',
"""Renames e.g. myimage_s1.tif to myimage_s01.tif""",
scripts.String(
"Data_Type", optional=False, grouping="1",
description="Choose source of images (only Dataset supported)",
values=data_types, default="Dataset"),
scripts.List(
"IDs", optional=False, grouping="2",
description="List of Dataset IDs").ofType(rlong(0)),
scripts.String(
"Number_Prefix", optional=False, grouping="3",
description="E.g. '_s' to rename myimage_s1.tif to myimage_s01.tif"),
)
try:
script_params = client.getInputs(unwrap=True)
conn = BlitzGateway(client_obj=client)
message = rename_images(conn, script_params)
client.setOutput("Message", rstring(message))
finally:
client.closeSession()
if __name__ == "__main__":
run_script()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment