Slide I/O
Functions
Methods and classes to read and write slides in the .ome.tiff format
- valis.slide_io.convert_to_ome_tiff(src_f, dst_f, level, series=None, xywh=None, colormap=None, tile_wh=None, compression='lzw')[source]
Convert an image to an ome.tiff image
Saves a new copy of the image as a tiled pyramid ome.tiff with valid ome-xml. Uses pyvips to save the image. Currently only writes a single series.
- Parameters:
src_f (str) – Path to image to be converted
dst_f (str) – Path indicating where the image should be saved.
level (int) – Pyramid level to be converted.
series (str) – Series to be converted.
xywh (tuple of int, optional) – The region of the slide to be converted. If None, then the entire slide will be converted. Otherwise xywh is the (top left x, top left y, width, height) of the region to be sliced.
colormap (dict, optional) – Dictionary of channel colors, where the key is the channel name, and the value the color as rgb255. If None (default), the channel colors from current_ome_xml_str will be used, if available. If None, and there are no channel colors in the current_ome_xml_str, then no colors will be added
tile_wh (int) – Tile shape used to save the image. Used to create a square tile, so tile_wh is both the width and height.
compression (str) – Compression method used to save ome.tiff . Default is lzw, but can also be jpeg or jp2k. See pyips for more details.
- valis.slide_io.create_ome_xml(shape_xyzct, bf_dtype, is_rgb, pixel_physical_size_xyu=None, channel_names=None, colormap=None)[source]
Create new ome-xmml object
- Parameters:
bf_dtype (str) – String format of Bioformats datatype
is_rgb (bool) – Whether or not the image is RGB
pixel_physical_size_xyu (tuple, optional) – Physical size per pixel and the unit.
channel_names (list, optional) – List of channel names.
colormap (dict, optional) – Dictionary of channel colors, where the key is the channel name, and the value the color as rgb255. If None (default), the channel colors from current_ome_xml_str will be used, if available.
- Returns:
new_ome – ome_types.model.OME object containing ome-xml metadata
- Return type:
ome_types.model.OME
- valis.slide_io.get_slide_reader(src_f, series=None)[source]
Get appropriate SlideReader
If a slide can be read by openslide and bioformats, VipsSlideReader will be used because it can be opened as a pyvips.Image. More common formats, like png, jpeg, etc… will be opened with scikit-image. Everything else will be opened with Bioformats.
- Parameters:
- Returns:
reader – SlideReader class that can read the slide and and convert them to images or pyvips.Images at the specified level and series. They also contain a MetaData object that contains information about the slide, like dimensions at each level, physical units, etc…
- Return type:
Notes
pyvips will be used to open ome-tiff images when series is 0
- valis.slide_io.init_jvm(jar=None, mem_gb=10)[source]
Initialize JVM for BioFormats
- Parameters:
mem_gb (int) – Amount of memory, in GB, for JVM
- valis.slide_io.save_ome_tiff(img, dst_f, ome_xml=None, tile_wh=1024, compression='lzw')[source]
Save an image in the ome.tiff format using pyvips
- Parameters:
img (pyvips.Image, ndarray) – Image to be saved. If a numpy array is provided, it will be converted to a pyvips.Image.
ome_xml (str, optional) – ome-xml string describing image’s metadata. If None, it will be createdd
tile_wh (int) – Tile shape used to save img. Used to create a square tile, so tile_wh is both the width and height.
compression (str) – Compression method used to save ome.tiff . Default is lzw, but can also be jpeg or jp2k. See pyips for more details.
- valis.slide_io.update_xml_for_new_img(current_ome_xml_str, new_xyzct, bf_dtype, is_rgb, series, pixel_physical_size_xyu=None, channel_names=None, colormap=None)[source]
Update dimensions ome-xml metadata
Used to create a new ome-xmlthat reflects changes in an image, such as its shape
If current_ome_xml_str is invalid or None, a new ome-xml will be created
- Parameters:
current_ome_xml_str (str) – ome-xml string that needs to be updated
bf_dtype (str) – String format of Bioformats datatype
is_rgb (bool) – Whether or not the image is RGB
pixel_physical_size_xyu (tuple, optional) – Physical size per pixel and the unit.
channel_names (list, optional) – List of channel names.
colormap (dict, optional) – Dictionary of channel colors, where the key is the channel name, and the value the color as rgb255. If None (default), the channel colors from current_ome_xml_str will be used, if available. If None, and there are no channel colors in the current_ome_xml_str, then no colors will be added
- Returns:
new_ome – ome_types.model.OME object containing ome-xml metadata
- Return type:
ome_types.model.OME
Classes
MetaData
SlideReader
- class valis.slide_io.SlideReader(src_f, *args, **kwargs)[source]
Read slides and get metadata
- create_metadata()[source]
Create and fill in a MetaData object
- Returns:
metadata – MetaData object containing metadata about slide
- Return type:
- slide2image(level, xywh=None, *args, **kwargs)[source]
Convert slide to image
- Parameters:
- Returns:
img – An image of the slide or the region defined by xywh
- Return type:
ndarray
BioFormatsSlideReader
- class valis.slide_io.BioFormatsSlideReader(src_f, series=None, *args, **kwargs)[source]
Bases:
SlideReader
Read slides using BioFormats
Uses the packages jpype and bioformats-jar
- create_metadata()[source]
Create and fill in a MetaData object
- Returns:
metadata – MetaData object containing metadata about slide
- Return type:
- get_channel(level, series, channel)
Get channel from slide
- get_tiles_parallel(level, tile_bbox_list, pixel_type, series=0)[source]
Get tiles to slice from the slide
- scale_physical_size(level)
Get resolution pyramid level
Scale resolution to be for requested pyramid level
- property series
Slide series
- slide2image(level, series=None, xywh=None, *args, **kwargs)[source]
Convert slide to image
- Parameters:
- Returns:
img – An image of the slide or the region defined by xywh
- Return type:
ndarray
- slide2vips(level, series=None, xywh=None, tile_wh=None, *args, **kwargs)[source]
Convert slide to pyvips.Image
This method uses Bioformats to slice tiles from the slides, and then stitch them together using pyvips.
- Parameters:
level (int) – Pyramid level
series (int, optional) – Series number. Defaults to 0
xywh (tuple of int, optional) – The region to be sliced from the slide. If None, then the entire slide will be converted. Otherwise xywh is the (top left x, top left y, width, height) of the region to be sliced.
tile_wh (int, optional) – Size of tiles used to contstruct vips_slide
- Returns:
vips_slide – An of the slide or the region defined by xywh
- Return type:
pyvips.Image
VipsSlideReader
- class valis.slide_io.VipsSlideReader(src_f, *args, **kwargs)[source]
Bases:
SlideReader
Read slides using pyvips Pyvips includes OpenSlide and so can read those formats as well.
Notes
When using openslide, lower levels can only be read without distortion, if pixman version 0.40.0 is installed. As of Oct 7, 2021, Macports only has pixman version 0.38, which produces distorted lower level images. If using macports may need to install from source do “./configure –prefix=/opt/local/” when installing from source.
- create_metadata()[source]
Create and fill in a MetaData object
- Returns:
metadata – MetaData object containing metadata about slide
- Return type:
- get_channel(level, series, channel)
Get channel from slide
- scale_physical_size(level)
Get resolution pyramid level
Scale resolution to be for requested pyramid level
- slide2image(level, xywh=None, *args, **kwargs)[source]
Convert slide to image
- Parameters:
- Returns:
img – An image of the slide or the region defined by xywh
- Return type:
ndarray
FlattenedPyramidReader
- class valis.slide_io.FlattenedPyramidReader(src_f, *args, **kwargs)[source]
Bases:
VipsSlideReader
Read flattened pyramid using pyvips Read slide pyramids where each page/plane is a channel in the pyramid. An example would be one where the plane dimensions are something like [(600, 600), (600, 600), (600, 600), (300, 300), (300, 300), (300, 300)] for a 3 channel image with 2 pyramid levels. It seems that bioformats does not recognize these as pyramid images.
- create_metadata()[source]
Create and fill in a MetaData object
- Returns:
metadata – MetaData object containing metadata about slide
- Return type:
- get_channel(level, series, channel)
Get channel from slide
- scale_physical_size(level)
Get resolution pyramid level
Scale resolution to be for requested pyramid level
- slide2image(level, xywh=None, *args, **kwargs)[source]
Convert slide to image
- Parameters:
- Returns:
img – An image of the slide or the region defined by xywh
- Return type:
ndarray
ImageReader
- class valis.slide_io.ImageReader(src_f, *args, **kwargs)[source]
Bases:
SlideReader
Read image using scikit-image
- create_metadata()[source]
Create and fill in a MetaData object
- Returns:
metadata – MetaData object containing metadata about slide
- Return type:
- get_channel(level, series, channel)
Get channel from slide
- scale_physical_size(level)
Get resolution pyramid level
Scale resolution to be for requested pyramid level
- slide2image(xywh=None, *args, **kwargs)[source]
Convert slide to image
- Parameters:
- Returns:
img – An image of the slide or the region defined by xywh
- Return type:
ndarray