Method to pad the metadata well number?


Is there a way to pad the well number with a leading zero so that all wells are 3 characters long? I want A03 instead of A3.

My input images are named like so: “A - 7(fld 1 wv UV - DAPI).tif”, and my metadata module is able to parse all the relevant bits. But the Metadata_Well column in my output ends up looking like this:


which makes ordering and displaying things outside of CP later much more complicated.


Bump in case someone has an idea about this.


I don’t think CP has a way to do this, no; there was a ‘RenameOrRenumber’ module, but the potential for user error or malicious mischief causing havoc was pretty significant, so it won’t be included in CP going forward. You’d be better off using a script in python (or something equivalent) to do your digit padding, sorry.


It seems to me that a “normalize well padding” option in the Metadata module would not be very intrusive and get around the problems that could be created by a standalone module that lets the user manipulate things more willy-nilly…

I’m already scripting some of my postprocessing after CP, but this is one item that would be useful to have in CP directly


I think that’s a totally reasonable request- generally speaking, the best way to get our software engineer’s attention is to make a request on GitHub, so I’d suggest doing that.



Hi @nieder

I know this is only a stop gap and nowhere near a general solution based on natural sorting, but it follows the idea of simply renaming the files before using them in CellProfiler. I had to do this recently for folders of exported incycte files.

Here’s the code I used, you’re welcome to adapt it for your own purpose.

import glob
import os
import fnmatch
import time
import datetime

def get_tree(directory,match="*.tif",ignore="xml",stop=None):
    matches = []
    startinglevel = directory.count(os.sep)

    for root,dirnames,filenames in os.walk(directory):
        level = root.count(os.sep) - startinglevel
        if stop is not None and level > stop:
        for dirname in fnmatch.filter(filenames,match):
            pn = os.path.join(root,dirname)
            if not ignore in pn:

    return matches 

def convert_fn(pf):
    #input like VID288_F4_1_2017y03m25d_02h35m.tif
    #output like VID288_F04_0001_1490409300.tif
    p,fn = os.path.split(pf)
    fn_list = fn[:-4].split("_",3)
    well = fn_list[1]
    fn_list[1] = well[0]+"%02d" % int(well[1:])
    fn_list[2] = "%04d" % int(fn_list[2])
    fn_list[3] = str(int(time.mktime(
        datetime.datetime.strptime(fn_list[3], "%Yy%mm%dd_%Hh%Mm").timetuple())))

    fn = "_".join(fn_list)+".tif"
    return os.path.join(p,fn)

if __name__ == '__main__':
    directory = r"D:\Data\INCUCITE DATA FOR EGOR"
    filenames = get_tree(directory, match="VID*.tif")

    for fn in filenames:
        if not "m.tif" in fn:

        print fn
        fn_out = convert_fn(fn)
        os.rename(fn, fn_out)

Then my regular expression in Metadata is: