# Buggy code
def get_filename(self, workspace, make_dirs=True, check_overwrite=True):
    "Concoct a filename for the current image based on the user settings"

    measurements = workspace.measurements
    if self.file_name_method == FN_SINGLE_NAME:
        filename = self.single_file_name.value
        filename = workspace.measurements.apply_metadata(filename)
    elif self.file_name_method == FN_SEQUENTIAL:
        filename = self.single_file_name.value
        filename = workspace.measurements.apply_metadata(filename)
        n_image_sets = workspace.measurements.image_set_count
        ndigits = int(np.ceil(np.log10(n_image_sets + 1)))
        ndigits = max((ndigits, self.number_of_digits.value))
        padded_num_string = str(measurements.image_set_number).zfill(ndigits)
        filename = "%s%s" % (filename, padded_num_string)
    else:
        file_name_feature = self.source_file_name_feature
        filename = measurements.get_current_measurement("Image", file_name_feature)
        filename = os.path.splitext(filename)[0]
        if self.wants_file_name_suffix:
            suffix = self.file_name_suffix.value
            suffix = workspace.measurements.apply_metadata(suffix)
            filename += suffix

    filename = "%s.%s" % (filename, self.get_file_format())
    pathname = self.pathname.get_absolute_path(measurements)
    if self.create_subdirectories:
        image_path = self.source_path(workspace)
        subdir = relpath(image_path, self.root_dir.get_absolute_path())
        pathname = os.path.join(pathname, subdir)
    if len(pathname) and not os.path.isdir(pathname) and make_dirs:
        try:
            os.makedirs(pathname)
        except:
            #
            # On cluster, this can fail if the path was created by
            # another process after this process found it did not exist.
            #
            if not os.path.isdir(pathname):
                raise
    result = os.path.join(pathname, filename)
    if check_overwrite and not self.check_overwrite(result, workspace):
        return

    if check_overwrite and os.path.isfile(result):
        os.remove(result)
    return result


# Traceback (WindowsError)
Traceback (most recent call last):
File "D:\Trunk\CellProfiler\cellprofiler\pipeline.py", line 1947, in run_image_set
self.run_module(module, workspace)
File "D:\Trunk\CellProfiler\cellprofiler\pipeline.py", line 2058, in run_module
module.run(workspace)
File "D:\Trunk\CellProfiler\cellprofiler\modules\saveimages.py", line 483, in run
should_save = self.run_image(workspace)
File "D:\Trunk\CellProfiler\cellprofiler\modules\saveimages.py", line 527, in run_image
self.save_image(workspace)
File "D:\Trunk\CellProfiler\cellprofiler\modules\saveimages.py", line 719, in save_image
filename = self.get_filename(workspace)
File "D:\Trunk\CellProfiler\cellprofiler\modules\saveimages.py", line 874, in get_filename
os.remove(result)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: u'C:\\Users\\Developer\\Desktop\\random_grayscale_image_00_B.tif'

# Fixed code
def get_filename(self, workspace, make_dirs=True, check_overwrite=True):
    "Concoct a filename for the current image based on the user settings"

    measurements = workspace.measurements
    if self.file_name_method == FN_SINGLE_NAME:
        filename = self.single_file_name.value
        filename = workspace.measurements.apply_metadata(filename)
    elif self.file_name_method == FN_SEQUENTIAL:
        filename = self.single_file_name.value
        filename = workspace.measurements.apply_metadata(filename)
        n_image_sets = workspace.measurements.image_set_count
        ndigits = int(np.ceil(np.log10(n_image_sets + 1)))
        ndigits = max((ndigits, self.number_of_digits.value))
        padded_num_string = str(measurements.image_set_number).zfill(ndigits)
        filename = "%s%s" % (filename, padded_num_string)
    else:
        file_name_feature = self.source_file_name_feature
        filename = measurements.get_current_measurement("Image", file_name_feature)
        filename = os.path.splitext(filename)[0]
        if self.wants_file_name_suffix:
            suffix = self.file_name_suffix.value
            suffix = workspace.measurements.apply_metadata(suffix)
            filename += suffix

    filename = "%s.%s" % (filename, self.get_file_format())
    pathname = self.pathname.get_absolute_path(measurements)
    if self.create_subdirectories:
        image_path = self.source_path(workspace)
        subdir = relpath(image_path, self.root_dir.get_absolute_path())
        pathname = os.path.join(pathname, subdir)
    if len(pathname) and not os.path.isdir(pathname) and make_dirs:
        try:
            os.makedirs(pathname)
        except:
            #
            # On cluster, this can fail if the path was created by
            # another process after this process found it did not exist.
            #
            if not os.path.isdir(pathname):
                raise
    result = os.path.join(pathname, filename)
    if check_overwrite and not self.check_overwrite(result, workspace):
        return

    if check_overwrite and os.path.isfile(result):
        try:
            os.remove(result)
        except:
            import bioformats

            bioformats.clear_image_reader_cache()
            os.remove(result)
    return result