Page MenuHomec4science

slide.py
No OneTemporary

File Metadata

Created
Sat, Jul 20, 06:44

slide.py

import openslide
from openslide import OpenSlide, OpenSlideError
import os
import json
class Slide:
def __init__(self, imagepath):
self.osr = OpenSlide(imagepath)
# Microns per pixel y
def mpp_y(self):
return self.osr.properties.get(openslide.PROPERTY_NAME_MPP_X)
# Microns per pixel x
def mpp_x(self):
return self.osr.properties.get(openslide.PROPERTY_NAME_MPP_Y)
# Pixels mer milimeter
def px_mm(self):
if self.mpp_x() is not None and self.mpp_y() is not None:
return 1 / (((float(self.mpp_x()) + float(self.mpp_y())) / 2) / 1000)
else:
return None
# Pixels mer milimeter
def px_m(self):
if self.px_mm() is not None:
return self.px_mm() * 1000
else:
return None
# Vendor
def vendor(self):
return self.osr.properties.get(openslide.PROPERTY_NAME_VENDOR)
# X coordinate of the rectangle bounding the non-empty region of the slide, if available
def bounds_x(self):
return self.osr.properties.get(openslide.PROPERTY_NAME_BOUNDS_X)
# X coordinate of the rectangle bounding the non-empty region of the slide, if available
def bounds_y(self):
return self.osr.properties.get(openslide.PROPERTY_NAME_BOUNDS_Y)
# width of the rectangle bounding the non-empty region of the slide, if available.
def bounds_width(self):
return self.osr.properties.get(openslide.PROPERTY_NAME_BOUNDS_WIDTH)
# Height of the rectangle bounding the non-empty region of the slide, if available.
def bounds_height(self):
return self.osr.properties.get(openslide.PROPERTY_NAME_BOUNDS_HEIGHT)
# x, y and rectangle width & height bounding the non-empty region of the slide, if available. JSON
def bounds(self):
info = {}
info['x'] = self.bounds_x()
info['y'] = self.bounds_y()
info['width'] = self.bounds_width()
info['height'] = self.bounds_height()
return json.dumps(info)
# Number of magnification levels
def nbr_levels(self):
return self.osr.level_count
# A list of downsample factors for each level of the slide. level_downsamples[k] is the downsample factor of level k.
def level_downsamples(self):
return self.osr.level_downsamples
# A list of (width, height) tuples, one for each level of the slide. level_dimensions[k] are the dimensions of level k.
def level_dimensions(self):
return self.osr.level_dimensions
# Best level for displaying the given downsample.
def best_level_for_downsample(self, downsample):
return self.osr.get_best_level_for_downsample(float(downsample))
# Magnification level
def magnification(self):
return self.osr.properties.get(openslide.PROPERTY_NAME_OBJECTIVE_POWER)
def common_info(self):
info = {}
info['pmm'] = self.px_mm()
info['pm'] = self.px_m()
info['nbrlevels'] = self.nbr_levels()
info['leveldownsamples'] = self.level_downsamples()
info['leveldimensions'] = self.level_dimensions()
info['bounds'] = {}
info['bounds']['x'] = self.bounds_x()
info['bounds']['y'] = self.bounds_y()
info['bounds']['width'] = self.bounds_width()
info['bounds']['height'] = self.bounds_height()
return json.dumps(info)

Event Timeline