Helper functions to extract, view and store metadata from the header of SEM images (.tif) recorded using either Zeiss SmartSEM or Thermo Fisher Scientific xT software.
Please clone/download the repository and install with pip
cd sem_io
pip install .
Command line
To print an overview of parameters from the image header in the console, at the command line, you can do:
sem_io path/to/my/image.tif
To print an overview from several images one after the other, you can use:
sem_io path/to/my/image.tif path/to/my/image2.tif path/to/my/image3.tif
To print an overview from all images in a folder, you can use:
sem_io path/to/my/folder_of_tif_images
You can process any number of individual images and folders at the same time:
sem_io path/to/my/image.tif path/to/my/folder_of_tif_images path/to/my/image2.tif
The flag -d can be used to additionally dump selected metadata to json:
sem_io path/to/my/image.tif -d
The json file will be located in the same folder as the image and will have the name my_image_metadata.json. As before, you can process any number of images and/or folders of images with the flag -d.
To dump metadata from a folder containing many images, without printing the output to the screen, you can additionally use the flag -s:
sem_io path/to/my/folder_of_tif_images -d -s
or, more simply:
sem_io path/to/my/folder_of_tif_images -ds
Either of the above two commands will save a json file for each .tif image in the specified folder.
If you prefer a simple text file instead of a json, you can pipe the output of sem_io to file like this:
sem_io path/to/my/folder_of_tif_images > other/path/my_text_output.txt
The above command will put the output of sem_io for all the .tif images in the folder given into a single text file. You can equally do this for a single .tif image by specifying the path to an image instead of to a folder.
Python
You can also import the module and use the functions directly in Python.
To print an overview of parameters from the image header in the console:
>>> import sem_io
>>> my_params = sem_io.SEMparams(r"path/to/my/image.tif")
If you just want to collect and store the parameters and not print them, you can do:
>>> my_params = sem_io.SEMparams(r"path/to/my/image.tif", verbose=False)
You can print ALL the header parameters to the console like this:
>>> my_params.print_param_dict(my_params.params)
You can print A SELECTION OF the header parameters (given in the class definition) to the console like this:
>>> my_params.print_param_dict(my_params.params_grouped)
Both my_params.params and my_params.params_groups are dictionaries and any parameter can be accessed, e.g.
>>> date = my_params.params_grouped["General"]["Date"]
Alternatively you can dump all the header parameters to a json file (optionally including a key giving the original image path) like this:
>>> my_params.dump_params_to_json(my_params.params, r"my_json_path.json", image_path=my_params.img_path)
Calling the command line functionality from Python
You can call the functions from the command line within Python using the subprocess module. For example, to dump metadata from a directory of images to json and print the output to the Python terminal:
>>> import subprocess
>>> subprocess.run(["sem_io", r"path/to/my/folder_of_tif_images", "-d"])
To pipe the output of sem_io to a text file instead:
>>> with open(r"my/output/path/myfile.txt", "w") as output_file:
... process = subprocess.Popen(["sem_io", r"path/to/my/image.tif"], stdout=output_file)
... process.communicate()
...
All the functions are staticmethods, so you don't need to instantiate the SEMparams class at all. For example, there is a bespoke function for getting the image pixel size and its unit in one line of code:
>>> pixel_size, unit = sem_io.SEMparams.get_image_pixel_size(r"path/to/my/image.tif")
This is useful if you want to plot the SEM image using matplotlib and add a scalebar with the correct dimensions using matplotlib-scalebar. Here's the whole process as an example:
>>> import matplotlib.pyplot as plt
>>> from matplotlib_scalebar.scalebar import ScaleBar
>>> import sem_io
>>> my_image = plt.imread(r"path_to_my_image.tif")
>>> fig, ax = plt.subplots()
>>> ax.imshow(my_image, cmap='gray')
>>> pixel_size, unit = sem_io.SEMparams.get_image_pixel_size(r"path_to_my_image.tif")
>>> my_scalebar = ScaleBar(pixel_size, units=unit, location='lower right', scale_loc='top')
>>> ax.add_artist(my_scalebar)
Even Electron Channeling Patterns acquired in rocking beam mode are correctly handled by sem_io.SEMparams.get_image_pixel_size()
- The selected parameters defined in the class definition of SEMparams form a subset of those available in the header of the .tif image. If you are interested in other parameters, the program can be easily customised - all the header parameters are extracted and are available as the "params" instance attribute.
- If there are any issues, please feel free to get in touch using the issues mechanism