def splitimage(data, splitwidth, datafield, xreal, yreal, xres, yres): # get current number of images within file orig_ids = gwy.gwy_app_data_browser_get_data_ids(data) # Define the width of the image to crop to in pixels splitwidth_px = int((splitwidth / xreal) * xres) # Define the number of images to split into based on the resolution (in one direction) no_splits = int(round(xreal / splitwidth)) # iterate in both x and y to get coordinates to crop image to for x in range(no_splits): xmin = splitwidth_px * x if xmin < 0: xmin = 0 xmax = splitwidth_px * x + splitwidth_px if xmax > xres: xmax = xres for y in range(no_splits): ymin = splitwidth_px * y if ymin < 0: ymin = 0 ymax = splitwidth_px * y + splitwidth_px if ymax > yres: ymax = yres # coordinates to crop to tiles = xmin, ymin, xmax, ymax # duplicate image crop_datafield_i = datafield.duplicate() # crop image crop_datafield_i.resize(xmin, ymin, xmax, ymax) # add cropped datafield to active container gwy.gwy_app_data_browser_add_data_field(crop_datafield_i, data, i + (len(orig_ids))) # Generate list of datafields including cropped fields crop_ids = gwy.gwy_app_data_browser_get_data_ids(data) return orig_ids, crop_ids, data
def flush_memory(self): """Deletes all the data channels in a Gwyddion container. This is needed as the memory will fill up quickly and a C memory error will occur. """ for data_ch_id in gwy.gwy_app_data_browser_get_data_ids( self.container): self.key = gwy.gwy_app_get_data_key_for_id(data_ch_id) self.container.remove(self.key)
def choosechannels(data, channel1, channel2): # Obtain the data field ids for the data file (i.e. the various channels within the file) ids = gwy.gwy_app_data_browser_get_data_ids(data) # Make an empty array for the chosen channels to be saved into chosen_ids = [] # Find channels with the title ZSensor, if these do not exist, find channels with the title Height chosen_ids = gwy.gwy_app_data_browser_find_data_by_title(data, channel1) if not chosen_ids: chosen_ids = gwy.gwy_app_data_browser_find_data_by_title(data, channel2) else: chosen_ids = chosen_ids # Save out chosen_ids as a list of the channel ids for ZSensor or height if ZSensor doesnt exist return chosen_ids
def run(): c = gwy.gwy_app_data_browser_get_current(gwy.APP_CONTAINER) _id = gwy.gwy_app_data_browser_get_current(gwy.APP_DATA_FIELD_ID) # key = "/0/select/line" selections = [ key for key in c.keys_by_name() if "/{}/select".format(_id) in key ] # print(selections) # create new combo box combo = gtk.combo_box_new_text() # add in valid selection options for selection in selections: combo.append_text(selection) # set default value as first in selections list, otherwise -1: None combo.set_active(0 if len(selections) else -1) # set up generic dialogue window dialogue = gtk.Dialog( title="Choose Selection", flags=gtk.DIALOG_MODAL, buttons=( gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, gtk.STOCK_OK, gtk.RESPONSE_ACCEPT, ), ) # add combobox widget to dialogue dialogue.get_content_area().pack_start(combo) # main loop and wait for user response dialogue.show_all() response = dialogue.run() # heavy lifting done below here... # is user pressed OK if response == gtk.RESPONSE_ACCEPT: key = selections[combo.get_active()] # for each datafield in container for _id0 in gwy.gwy_app_data_browser_get_data_ids(c): # copy line line = c[key].duplicate() # add to new datafield with new key c.set_object( gwy.gwy_key_from_name(key.replace(str(_id), str(_id0))), line) # kill dialogue dialogue.destroy()
def run(container, mode): container = gwy.gwy_app_data_browser_get_current(gwy.APP_CONTAINER) filename = container['/filename'] path, _ = os.path.splitext(filename) output_path = path + OUTPUT_SUFFIX if not os.path.exists(output_path): os.mkdir(output_path) for i, id_ in enumerate(gwy.gwy_app_data_browser_get_data_ids(container)): title = container['/{}/data/title'.format(id_)] gwy.gwy_app_data_browser_select_data_field(container, id_) output_filename = '{:03d}_{}.{}'.format(id_, title, OUTPUT_FILESUFFIX) output_filename = os.path.join(output_path, output_filename) mode = gwy.RUN_INTERACTIVE if i == 0 else gwy.RUN_NONINTERACTIVE print 'Exporting {} ...'.format(output_filename) gwy.gwy_file_save(container, output_filename, mode)
def mul_split(file_path): """Splits all data channels into single gwy files""" m_id = data.m_id(file_path) dir_path = os.path.dirname(os.path.abspath(file_path)) con = gwy.gwy_app_file_load(file_path) ch_ids = gwy.gwy_app_data_browser_get_data_ids(con) def inner(int_id): # pylint: disable=missing-docstring new_container = gwy.Container() gwy.gwy_app_data_browser_add(new_container) gwy.gwy_app_data_browser_copy_channel(con, int_id, new_container) file_name = str(m_id) + "_" + str(int_id) + ".gwy" file_out = os.path.join(dir_path, file_name) meta_id = get_meta_ids(new_container)[0] time_extract = new_container[meta_id]["Date"] time_reformat = datetime.strptime(time_extract, "%Y-%m-%d %H:%M:%S") gwy.gwy_app_file_write(new_container, file_out) time_sec = time.mktime(time_reformat.timetuple()) os.utime(file_out, (time_sec, time_sec)) return file_out return [inner(ch_id) for ch_id in ch_ids]
def gwybas(filename): #add the gwyddion folders to the path, making sure they can be found import sys sys.path.append('C:\Program Files (x86)\Gwyddion\\bin') sys.path.append('C:\Program Files (x86)\Gwyddion\share\gwyddion\pygwy') #import gwyddion import gwy import gwyutils #load the file and add to data browser c = gwy.gwy_file_load(filename, gwy.RUN_IMMEDIATE) gwy.gwy_app_data_browser_add(c) #Set the right settings for the align_rows command settings = gwy.gwy_app_settings_get() settings['/module/linematch/direction'] = int(gwy.ORIENTATION_HORIZONTAL) settings['/module/linematch/do_extract'] = False settings['/module/linematch/do_plot'] = False settings[ '/module/linematch/method'] = 0 # 0: poly, 1: median, 2: median of diff,3: modus,4: matching, 5: trimemd mean, 6: trimmed mean of diff settings['/module/linematch/masking'] = 2 settings['/module/linematch/max_degree'] = 3 #Order of polynominal settings['/module/linematch/trim_fraction'] = 0.05 #print the datafield ID's corresponding to the different channels of the AFM, such as height, phase and error, usually the first one (0) is the height # print gwy.gwy_app_data_browser_get_data_ids(c) #itterate over the different datafield ID's/AFM channels, and do processing on them for datafield_id in gwy.gwy_app_data_browser_get_data_ids(c): # datafield = c['/%d/data' % datafield_id] #set the color range to automatic with tials cut off (corresponding to number 2) c['/%d/base/range-type' % datafield_id] = 2 #select the datafield_ID/AFM channel to process gwy.gwy_app_data_browser_select_data_field(c, datafield_id) #level the plane gwy.gwy_process_func_run("level", c, gwy.RUN_IMMEDIATE) #align the rows, with settings chosen above line 19-25 gwy.gwy_process_func_run("align_rows", c, gwy.RUN_IMMEDIATE) #remove scars a couple of times (button bashing) gwy.gwy_process_func_run("scars_remove", c, gwy.RUN_IMMEDIATE) gwy.gwy_process_func_run("scars_remove", c, gwy.RUN_IMMEDIATE) gwy.gwy_process_func_run("scars_remove", c, gwy.RUN_IMMEDIATE) gwy.gwy_process_func_run("scars_remove", c, gwy.RUN_IMMEDIATE) gwy.gwy_process_func_run("scars_remove", c, gwy.RUN_IMMEDIATE) #fix lowest point to zero gwy.gwy_process_func_run('fix_zero', c, gwy.RUN_IMMEDIATE) #define new file names, I chose to simply add the wanted extention to the original filename newname = filename + '.gwy' newname2 = filename + '.jpg' #find the ID/Channel corresponding to the height, of this one a JPG will be created, .gwy will contain all channels ids = gwy.gwy_app_data_browser_find_data_by_title(c, 'Height') gwy.gwy_app_data_browser_select_data_field(c, ids[0]) gwy.gwy_file_save(c, newname, gwy.RUN_NONINTERACTIVE) gwy.gwy_file_save(c, newname2, gwy.RUN_NONINTERACTIVE) #remove the current container, makes room for the next file gwy.gwy_app_data_browser_remove(c)
gwy.gwy_app_data_browser_add(container) # Save metadata to image.json image_folder = output_folder image_meta = {} image_meta["type"] = "inspect-gwy" meta = container["/0/meta"] for entry in meta.keys_by_name(): image_meta[entry] = meta[entry] #print('%s:%s' % (entry,meta[entry])) file = open("%s/image.json" % (image_folder), "w" ) json.dump(image_meta,file) file.close() # List all channels ids = gwy.gwy_app_data_browser_get_data_ids(container) for i in ids: channel_folder = "%s/channel/%02d/" % (output_folder,i) os.makedirs(channel_folder) print("Created folder '%s'" % channel_folder) # Channel metadata ch_meta = {} # Get channel title title = container["/%d/data/title" % i] ch_meta["title"] = title ch_meta["index"] = i # Select the channel gwy.gwy_app_data_browser_select_data_field(container, i)
def boundbox(cropwidth, datafield, grains, dx, dy, xreal, yreal, xres, yres): # Function to return the coordinates of the bounding box for all grains. # contains 4 coordinates per image bbox = datafield.get_grain_bounding_boxes(grains) # Remove all data up to index 4 (i.e. the 0th, 1st, 2nd, 3rd). # These are the bboxes for grain zero which is the background and should be ignored del bbox[:4] # Find the center of each grain in x center_x = datafield.grains_get_values(grains, gwy.GRAIN_VALUE_CENTER_X) # Delete the background grain center del center_x[0] # Find the center of each grain in y center_y = datafield.grains_get_values(grains, gwy.GRAIN_VALUE_CENTER_Y) # Delete the background grain center del center_y[0] # Find the centre of the grain in pixels # get active container data = gwy.gwy_app_data_browser_get_current(gwy.APP_CONTAINER) # get current number of files orig_ids = gwy.gwy_app_data_browser_get_data_ids(data) # Define the width of the image to crop to cropwidth = int((cropwidth / xreal) * xres) # make 2d array for grains multidim_grain_array = np.reshape(np.array(grains), (xres, yres)) for i in range(len(center_x)): px_center_x = int((center_x[i] / xreal) * xres) px_center_y = int((center_y[i] / yreal) * yres) # ULcol = px_center_x - cropwidth xmin = px_center_x - cropwidth # ULrow = px_center_y - cropwidth ymin = px_center_y - cropwidth # BRcol = px_center_x + cropwidth xmax = px_center_x + cropwidth # BRrow = px_center_y + cropwidth ymax = px_center_y + cropwidth # making sure cropping boxes dont run outside the image dimensions if xmin < 0: xmin = 0 xmax = 2 * cropwidth if ymin < 0: ymin = 0 ymax = 2 * cropwidth if xmax > xres: xmax = xres xmin = xres - 2 * cropwidth if ymax > yres: ymax = yres ymin = yres - 2 * cropwidth # print ULcol, ULrow, BRcol, BRrow # crop the data crop_datafield_i = datafield.duplicate() crop_datafield_i.resize(xmin, ymin, xmax, ymax) # add cropped datafield to active container gwy.gwy_app_data_browser_add_data_field(crop_datafield_i, data, i + (len(orig_ids))) # cropping the grain array: grain_num = i + 1 cropped_np_grain = multidim_grain_array[ymin:ymax, xmin:xmax] cropped_grain = [ 1 if i == grain_num else 0 for i in cropped_np_grain.flatten() ] # make a list containing each of the cropped grains try: cropped_grains.append(cropped_grain) except NameError: cropped_grains = [cropped_grain] # Generate list of datafields including cropped fields crop_ids = gwy.gwy_app_data_browser_get_data_ids(data) return bbox, orig_ids, crop_ids, data, cropped_grains, cropwidth
def return_data_ch_titles(self): """Returns data channel titel""" self.ch = gwy.gwy_app_data_browser_get_data_ids(self.container) return [self.container["/" + str(i) + "/data/title"] for i in self.ch]