Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
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
You can’t perform that action at this time.