Example #1
0
def mrc2stack(mrc,
              out_dir,
              indxs=None,
              basename="%04d.png",
              imfilter=lambda im: im):
    """
    Converts an MRC file to an image stack

    Arguments:
    mrc      -- the MRC file (as MRC object) or filepath (as a string)
    out_dir  -- the directory to save the stack to
    
    Optional Arguments:
    indxs     -- the indices of slices to save, default is to use all slices
    basename  -- the template name to use for images, needs to have a %d to be replaced by slice number, default is "%04d.png"
    imfilter  -- a function/callable that takes and returns an image
    """
    from os.path import join
    from mrc import MRC
    from images import imsave
    from utils import make_dir

    if isinstance(mrc, basestring): mrc = MRC(mrc)
    if not make_dir(out_dir):
        raise IOError("Unable to create output directory")
    if indxs == None:
        for i, sec in enumerate(mrc):
            imsave(join(out_dir, basename % i), imfilter(sec))
    else:
        for i in indxs:
            imsave(join(out_dir, basename % i), imfilter(mrc[i]))
Example #2
0
def stack2mrc(stack, mrc, imfilter=lambda im: im):
    """
    Converts an image stack to an MRC file. Returns the new MRC file object.

    Arguments:
    stack    -- the images to read, an iterable of file names
    mrc      -- the MRC filename to save to
    
    Optional Arguments:
    imfilter  -- a function/callable that takes and returns an image
    """
    from mrc import MRC
    from images import imread

    stack = iter(stack)
    try:
        img = imfilter(imread(stack.next()))
    except StopIteration:
        raise ValueError("Must provide at least one image")
    mrc = MRC(mrc, nx=img.shape[1], ny=img.shape[0], dtype=img.dtype)
    mrc.append(img)
    mrc.append_all(imfilter(imread(img))
                   for img in stack)  # will skip the first one
    mrc.write_header()
    return mrc
Example #3
0
            y = y.split("-")
            if len(y) != 2 or not y[0].isdigit() or not y[1].isdigit():
                help_msg(2, "Invalid y argument supplied")
            y = (int(y[0]), int(y[1]))
        else:
            imfilters += [imfilter_util.parse_opt(o, a, help_msg)]

    # Make sure paths are good
    if len(args) != 2:
        help_msg(
            2,
            "You need to provide an MRC and image output directory as arguments"
        )
    mrc_filename = realpath(args[0])
    try:
        mrc = MRC(mrc_filename, readonly=True)
    except BaseException as e:
        help_msg(2, "Failed to open MRC file: " + str(e))
    out_dir = realpath(args[1])
    if not make_dir(out_dir):
        help_msg(
            2,
            "Output directory already exists as regular file, choose another directory"
        )

    # Check other arguments, getting values for optional args, etc.
    ext = "png" if ext == None else ext.lstrip('.')
    if basename == None: basename = "%04d"
    if x == None: x = (0, mrc.nx - 1)
    elif x[0] < 0 or x[1] < x[0] or x[1] >= mrc.nx:
        help_msg(2, "Invalid x argument supplied")